- Home /
OnTriggerEnter Help?
Hello everybody,
I am currently working on a horror game in unity and I ran into a problem. If anyone can help me out with this, I would really appreciate it! In the game, the Player is going to be walking through a hall and there will be a tigger zone (OnTriggerEnter) right before an intersection in the hallway. When the player hits that trigger zone, a gameobject will rush through the intersection (In front of you.) So really what I'm asking is, Is there a way to make a gameobject move when you enter a trigger (OnTriggerEnter)? If there is a way to do this, I would really appreciate some help. If there isn't a way to do this, do any of you guys have a different solution? Thanks for your help! I REALLY appreciate it!
Answer by jdams · Dec 15, 2012 at 05:28 PM
On the game object that is moving simply add a script called runningScript with a boolean variable. Like this:
var run : boolean;
function Update(){ if(run){ //script here to move the object. } }
Then on the trigger setup this script:
var runningObject : GameObject;
function OnTriggerEnter(){ runningObject.GetComponent("runningScript").run = true; }
In the Inspector Panel Drag and drop the running object into the "RunningObject" area of the trigger script.
Answer by fafase · Dec 15, 2012 at 04:28 PM
the idea is to wrap the behavior of your passing guy into a boolean:
WaitingScript.cs
public bool isInTrigger = true;
void Update(){
if(inInTrigger)//Get The guy going
}
On the trigger box:
WaitingScript script;
void Start(){
script = GameObject.Find("WaitingGuy").GetComponent<WaintingScript>()
}
void OnTriggerEnter(Collider ohter){
if(other.gameObject.tag=="Player")script.isInTrigger = true;
}
The same in unityScript:
WaitingScript.js
var isInTrigger:boolean=true;
function Update(){
if(inInTrigger)//Get The guy going
}
On the trigger box:
var script:WaitingScript;
function Start(){
script = GameObject.Find("WaitingGuy").GetComponent(WaintingScript)
}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Player")script.isInTrigger = true;
}
So just to be 100% clear, I put the WaitingScript.cs on the guy (who will rush past the camera) and the "On the trigger box" script on the Trigger box?
Yep. Your player does not get anything here. I guess you know about the trigger box, it should be an empty game object with a collider box set to isTrigger.