The question is answered, right answer was accepted
Checking for collisions
So, I've set up a controlling script for a character of mine and here is the issue: I'm using "OnCollisionEnter/Exit" to check when to play a falling animation, landing animation and such, but whenever passing from one collider to another, the character does the landing animation even if both colliders are in contact with the character at once. How do I make it so that it checks for other colliders before setting the animation? Here is the code if it helps:`
{
Animator anim;
int jumpHash = Animator.StringToHash("jump");
int runStateHash = Animator.StringToHash("Base Layer.run");
int fightHash = Animator.StringToHash("fight");
int specfightHash = Animator.StringToHash("specfight");
int turnlHash = Animator.StringToHash("turnl");
int turnrHash = Animator.StringToHash("turnr");
int crouchHash = Animator.StringToHash("crouch");
int dropHash = Animator.StringToHash("drop");
int landingHash = Animator.StringToHash("landing");
public Animator Charanimator;
public CapsuleCollider character;
public Rigidbody rigid;
public Transform chartrans;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
float move = Input.GetAxis ("Vertical");
anim.SetFloat ("speed", move);
AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo (0);
if (Input.GetKeyDown (KeyCode.Space) && stateInfo.fullPathHash == runStateHash) {
anim.SetTrigger (jumpHash);
}
if (Input.GetKeyDown (KeyCode.Space)) {
anim.SetTrigger (jumpHash);
}
if (Input.GetKeyDown (KeyCode.Mouse0)) {
anim.SetTrigger (fightHash);
}
if (Input.GetKeyDown (KeyCode.Mouse1)) {
anim.SetTrigger (specfightHash);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
anim.SetTrigger (turnlHash);
}
if (Input.GetKey (KeyCode.RightArrow)) {
anim.SetTrigger (turnrHash);
}
if (Input.GetKey (KeyCode.C)) {
anim.SetTrigger (crouchHash);
}
if (Input.GetKey (KeyCode.LeftArrow) && stateInfo.fullPathHash == runStateHash)
{
chartrans.Rotate(Vector3.up * -50f* Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow) && stateInfo.fullPathHash == runStateHash)
{
chartrans.Rotate(Vector3.up * 50f* Time.deltaTime);
}
}
void OnCollisionExit (Collision col)
{
anim.SetTrigger (dropHash);
character.height = 1.52f;
character.center = Vector3.up * 1f;
}
void OnCollisionEnter (Collision col)
{
character.height = 1.84f;
character.center = Vector3.up * 0.92f;
anim.SetTrigger (landingHash);
}
} `
Answer by sb526 · Mar 10, 2017 at 12:56 AM
It's a good idea to use a double condition like in the example below. Check for collision AND for the change in the Character's velocity.y (y being the speed of change in height). This particular example checks if the playerController is NOT grounded and moving at negative Y velocity (falling) and if both conditions are met it enters the falling animation. Works for completing the jump and falling.
if(!playerController.IsGrounded() && playerController.velocity.y < 0)
{
animation.CrossFade("FallDown", 0.5);
animation["FallDown"].speed = 3;
}
Am I missing something? It tells me that there is no definition for ''IsGrounded''..
Never $$anonymous$$d, I figured it out. Thanks again.
Glad it helped! Can you mark it as answered? Trying to get my question through the moderation queue.
Follow this Question
Related Questions
Car will only turn right (& ignored collders) 0 Answers
Raycast does not seem to properly detect object it hit 0 Answers
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers
How do I make a blob? 0 Answers
How To how ?? 2 Answers