How to have a trigger activate animation on another game object.
In order to be able to accurate explain my problem, I will use an analogy.
If you have played Portal 2, you know that if you place a cube on a button, something will activate, say open a door. I want to have a similar mechanic. I know the button will be a trigger object and it will check, but how to connect the button being triggered to the door opening via C# scripts is what I don't know how to do.
I guess in short, activating another object through the script of another unrelated object (assume the two aren't colliding or near each other) is what I'm having trouble with.
Thanks! Let me know if you need ore information.
Thanks!
Answer by Guhanesh · Feb 12, 2016 at 10:54 AM
using UnityEngine;
using System.Collections;
public class TriggerScript: MonoBehaviour {
public Animation anim;
void OnTriggerEnter() {
ani.enabled = true;
}
Add this script to Trigger Object.Add Animation component of Door to anim in Inspector Disable Animation component on Door GameObject. or watch this https://www.youtube.com/watch?v=vL7CIvWxq4w
It's been a long time but I have another question for you. How would I play the same animation, but in reverse when the object triggering the script is no longer in contact with the trigger.
Say if this script was to be applied to the Portal 2 box and button mechanic I used earlier. I have it to where a door would open when the box comes into contact with the button, but I don't know how to close the door when the box is no longer in contact with the button.
Answer by hexagonius · Feb 08, 2016 at 09:25 PM
The easiest way would be to have one script on the trigger and one script on the door. the script on the trigger should have a public variable of type door script. In the inspector you should then be able to drag the door onto the variable of the trigger now visible in the inspector. The whole gameobject suffices, the script will be picked automatically instead. now you should be able to call upon public methods in the trigger script on the door script variable.
Thanks. Could you give me an example with code? I'm not very experienced with C#.