- Home /
Trigger animation using script on separate box collider
I'm trying to do something that I thought would be very simple, but it's proving to be very difficult to figure out.
I have a box collider with 'Is Trigger' enabled, and I want to add a script to this trigger box to play an animation on another game object. This game object has an animation on it, which was created in unity. I am using unity 4.3.4.
Example:
-Walk through underground tunnel where a collider box is entered.
-On entering the collider box a train animation is triggered
-Train drives down platform and stops (as animated)
Answer by pickleleon · Mar 06, 2014 at 10:11 PM
Thanks for your input, this worked for me. Just applied this script to the trigger box and dragged in the corresponding game objects.
var Train: GameObject; //object to be animated
var TrainAnimation : String; //animation to be played
function OnTriggerEnter(Collision){
Train.animation.Play(TrainAnimation);
}
After this I was getting a message about marking the animation as legacy. To do this with a unity made animation I found out I had to follow these steps.
Switch the Inspector panel to a debug mode (3 stripes in a right top corner, near lock icon)
Set the animation type to a "1"
Switch the Inspector panel to a normal mode
Answer by Levocakes · Feb 27, 2014 at 08:41 PM
I haven't used animations before in unity yet but I think this will help you to get started.
So you have a Box that has a script. When you walk through that "Box" which is a trigger it will make the animation play. I think something like this will help you.
The Train Script:
using UnityEngine;
using System.Collections;
public class TrainScript: MonoBehaviour {
public bool PlayAnimation = false;
void Start () {
}
void Update () {
if(PlayAnimation){
// Whatever function or code you write to start the animation
// or however you want to do the animation}
}
}
This Box Script :
using UnityEngine;
using System.Collections;
public class Box: MonoBehaviour {
// Make sure you have the Train Object
public GameObject Train;
// The Script of the train;
private TrainScript trainscript;
void Start () {
// This will get the script and set the trainscript
trainscript= Train.GetComponent("TrainScript ") as TrainScript ;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider collision){
if(collision.gameObject.tag == "Your Character's Tag"){
trainscript.PlayAnimation = true;
}
}
}
Now what this should do is, when your mainplayer/character enters the Box/Trigger it will edit the bool inside the train's script to make it true. Then inside of the trainscript you will have a case if that bool is true it will make the animation run/play.
Is there a way of calling a component on a separate game object without putting scripts on both? And I wanted to know the script for playing an animation too (not sure if it's different on later versions of Unity).