- Home /
Make a script begin its action OnTriggerEnter? Please help.
Hello everybody,
Before I even ask this question, I want to thank you for taking the time to look at this question, I really appreciate. Alrighty then, let's get to the question:
I am trying to make an object move when I enter a Trigger Zone (Cube with "Is Trigger" ticked.) I have a script that allows me to move an object from point A to point B, but I want this script to begin when I enter the trigger. Here is the script:
var endPoint : Vector3;
var duration : float = 1.0;
private var startPoint : Vector3;
private var startTime : float;
function Start() {
startPoint = transform.position;
startTime = Time.time;
}
function Update () {
transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
}
Is there any way to make this script begin (move the object) when I enter a Trigger Zone? If anyone could help me out with this, I would seriously appreciate it! Thanks!
Answer by DaveA · Jan 05, 2013 at 06:19 PM
var endPoint : Vector3;
var duration : float = 1.0;
private var startPoint : Vector3;
private var startTime : float = 0f;
function OnTriggerEnter () {
startPoint = transform.position;
startTime = Time.time;
}
function Update () {
if (startTime > 0f)
transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
}
Do I add this to the Trigger Box, or to the object I am trying to move? Thanks for your help, man!
Your answer
![](https://koobas.hobune.stream/wayback/20220613090448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Where to attach a script? 1 Answer
Collider doesn't transfer to function 1 Answer
Scripting help? 2 Answers
Procedurally generating terrain? 0 Answers