Trying to trigger animation with a collision with no luck Help Please!

Here’s the code I have been using, although, honestly, I have tried a lot of variations with no luck. The code causes no errors but simply doesn’t trigger any animation.

Help!

Animator animator;
bool nSand;

void Start()
{
animator = gameObject.GetComponent<Animator>();
nSand = false;
}

void Update()
{
void OnCollisionEnter2D(Collision2D other)

    {
    LandonController player = other.gameObject.GetComponent<LandonController >();
    if (player != null)
    {
    nSand = true;
    animator.SetTrigger("Sand");
    }
    else
    {
    nSand = false;
    animator.SetTrigger("Idle");
   
   
   
   
    }
    }
    }

Throw in some Debug.Logs to see if it is even calling. Also, for OnCollisionEnter2D you need to make sure both objects have a 2D collider and one of them needs a Rigidbody2D.

1 Like

Yes, both have 2D colliders and RigidBody2D. I did put in Debug.Log and it did not register at all.

Oh, it looks like you put OnCollisionEnter2D inside your Update function. You need to have it as its own function outside of the Update { }. Try that and see if your Debug.Logs get called.

1 Like

Thank you so much!!! That finally worked!!!

1 Like