- Home /
Question by
NothingGamer · Aug 13, 2017 at 10:00 PM ·
c#oncollisionenter
How can I check if im landing?
I want to make a script that my player starts playing a falling animation when he is not landing
but I don't know how to do it
I guess I have to use onCollisionEnter() but I dont know how
I know how to use animator, my character moves but i dont know how to check it
thank you for the help
Comment
Answer by unidad2pete · Aug 13, 2017 at 10:09 PM
You have some options.
public float onFloorY;
void Start()
{
onFloorY = transform.position.y;
}
public void CheckFloor()
{
if(transform.position.y > onFloor)
{
Debug.Log("is in the air");
} else
{
Debug.Log("is on floor");
}
}
it works but if I climb something he is going to fall
Try this;
public bool onFloor;
public void OnCollisionStay(Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "TAG NA$$anonymous$$E FLOOR")
{
onFloor = true;
}
}
public void OnCollisionExit(Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "TAG NA$$anonymous$$E FLOOR")
{
onFloor = false;
}
}
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionStay.html
Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.