Use of collision

Hi, i wrote this code :

void OnCollisionEnter (Collision col)

{

       if (col.gameObject.name == "Background")

                anim.SetBool ("IsFlying", false);

}

I need to pass as parameter something that identify the terrain collision.
But i don’t know how use the function OnCollisionEnter… Can someone give me some examples ?
Thank you so much

1 Answer

1

It would be easier on you to use a downward raycast.

RaycastHit hit;
Ray ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray, out hit))
{
   if(hit.collider.CompareTag("Terrain")){}
}

I tryed to put it in my code, but nothing has happened...

–

void Update(){ RaycastHit hit; Ray ray = new Ray(transform.position, Vector3.down); if(Physics.Raycast(ray, out hit)){ print(hit.collider.name); } } see what it prints. If ti prints "Player", you need to set the player to ignore raycast. Layer-> Ignore raycast. But for some reasons, it seems that lately inner raycast ignores the collider it is starting from inside...

–

In the console: Tag: Background is not defined! prova.Update() But Background is not on my character: There are Camere, Background and Character

–

CompareTag uses the tag of the object not the name. Background is probably a name while it should be a tag. Just add it as a tag and assign it to the object.

–

i have a character that can fly, but i'm trying to "lock" (false) the fly if he is touching the ground.. but now everytime i fly the fly mode is put into false...

–