r/Unity3D•
speccius

How do I trigger an animation using a Cube Collider?

Question

I've done this before in the past, where I have a cube set up as a trigger and when my player character collides with the cube, it triggers an animation on another game object. I just cannot get it to work. I've looked up multiple posts about this issue and none of the solutions in those posts either on here or the Unity Forums seems to work.

I would appreciate if someone could walk me through it. Relative beginner here.

Posted Sep 9, 2020, 10:19 AM UTC

08
CrossMountain•

Create an OnTriggerEnter, test with Debug.Log if it triggers. If it does, expand the function with GetComponent to get the Animator and play the animation.

2
specciusOP•

Ok. I'll give that a go and see how I get on.

Thanks!

1
specciusOP•

I tried setting that up but it didn't work. It came up with the message in the Debug Log the first time I ran it but now it doesn't do anything. That's the script I have attached to my trigger. I'm a coding newbie.

using System.Collections;

using UnityEngine;

​

public class InfoTrigger : MonoBehaviour {

​

public void OnTriggerEnter(Collider other){

​

Debug.Log("Info Triggered" + gameObject.name);

​

}

}

1
CrossMountain•

It's a bit difficult to find an error without insight into the project. Did you change anything that might be a reason for why the collision only worked in one test-run?

1
specciusOP•

Well the message appeared in the console right at the start so I dunno if the trigger was actually detected or it just displayed the message anyway. Basically, a bit of insight into the project, I'm creating a walkthrough of a virtual space and I'm wanting text bubbles to appear next to objects displaying info about them and I figured it'd be easier to have them appear using animation triggers like I had in the past but it's not doing what I want it to.

I have my animation attached to the text bubble object, a cube collider set up near it where the player controller will pass through and I want that collision to trigger the animation on the info bubble.

1
CrossMountain•

Sounds to me as if collision happens right at the start which sounds like a bad setup of Collider, Rigidbody and settings (kinetic Rigidbody, trigger on Collider set true). Also, I'm not sure, but I think you have to go other.gameObject.name if you want the name of the object entering the trigger. Better just print out a string to minimalize possible errors.

1