Triggering animation? I'm a total begginner...

Hey everyone, I’ve been reading a ton of stuff on these forums for as long as I could and tried a few things but couldn’t really fix my (or even begin to go about) my problem. See, what I’m trying to do is this:

There is a gate, there is a target (a sphere) floating, I throw a rock, it hits the sphere and the gate opens.

I don’t have any code to show because I’ve tried a few things but they don’t seem to work. I’m just sort of asking of a good way to go about it. I tried using BroadcastMessage but it only confused me. Just looking for a few tips as all.

Thanks so much to anyone in advance! After reading a lot of posts this really is an incredibly friendly community (and very smart).

Not sure exactly what you’re looking for, but it sounds like you’re just trying to trigger something (doesn’t necessarily have to be animation).

First of all- I’m not sure why so many tutorials insist on teaching BroadcastMessage, because A) it’s not always easy to explain how it works, and B) it’s about 100x slower than calling a function by name.

Here is a rough overview of how I would do what you’re trying to do (note the rough part, you can’t copy and paste this and expect it to do anything):

Sphere needs a SphereCollider component with IsTrigger set to true.
Your “Rock” needs a SphereCollider component, along with a RigidBody with IsKinematic set to true. You should also tag your rock with something relevant, like “Key” because it sounds like you’re using it like a key to a door.

Sounds like you have the ‘rock tossing’ part down, so I won’t go into that.

Your Sphere needs a script attached to it that will handle the trigger event. Inside the script, you will need to use the OnTriggerEnter event. It should look something like this:

void OnTriggerEnter(Collider collisionInfo) 
    if(collisionInfo.tag == "Key")
        Debug.Log("The Rock hit the sphere, now do whatever it is you wanted to do!");

That should hopefully help you out.


OK, so what you have to do, is create two scripts…

First of all, call this script “playAnimation”:

function ShowAndWaitUntilHide(){



Attach this to your gate object, along with the animation that opens the gate… where it says “openGate” type the name of the gate animation.

Then attach this to your target (sphere):

var gate : GameObject;

function OnCollisionEnter(collision : Collision){
	if (collision.gameObject.tag == "rock"){

Just make sure that your rock has been tagged “rock”…

Also, make sure that thet both have colliders and that one has a non kinematic rigidbody (you can untick :Use Gravity" if you don’t want it to have gravity…

Hope this helps…

comment back if you need more help!!! :smiley: