How do I make a object move between two positions? and whats wrong with my code.
I have two empty game objects that are acting as my trigger points, wp1 (Waypoint 1) and wp2 (Waypoint 2), this is my code.
It's moving from wp1 to wp2 with 2 seconds then jumping straight back to wp1 and repeating again.
I need to move the object between wp1 and wp2 so move from wp1 to wp2 then move back to wp1 and so on.
#pragma strict
// Transforms to act as start and end markers for the journey.
var startMarker: Transform;
var endMarker: Transform;
// Movement speed in units/sec.
var speed = 2.0;
// Time when the movement started.
private var startTime: float;
// Total distance between the markers.
private var journeyLength: float;
//How much distance has been covered so far
var fracJourney : float;
// Follows the target position like with a spring
var distCovered : float;
function Start ()
{
// Keep a note of the time the movement started.
startTime = Time.time;
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
distCovered = (Time.time - startTime) * speed;
}
function Update () {
// Distance moved = time * speed.
distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
fracJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}
function OnTriggerEnter(objCollider : Collider)
{
if (objCollider.gameObject.name == "wp1")
{
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}
else if (objCollider.gameObject.name == "wp2")
{
transform.position = Vector3.Lerp(endMarker.position, startMarker.position, fracJourney);
}
}
Answer by TBruce · Oct 12, 2016 at 08:07 PM
You can do it with the following script
#pragma strict
// Transforms to act as start and end markers for the journey.
var startMarker: Transform;
var endMarker: Transform;
// Movement speed in units/sec.
var speed = 2.0;
// the wait time between waypoints
var waitTimeBetweenWaypoints : float = 0.25;
function Start ()
{
Patrol();
}
function Patrol()
{
while (true)
{
var time : float = 0;
var lerpValue : float;
// set the position of to the startMarker)
transform.position = startMarker.position;
// move object from startMarker to endMarker)
while ((transform.position != endMarker.position) && (time < speed))
{
time += Time.deltaTime;
lerpValue = (time / speed);
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, lerpValue);
yield;
}
// set the position of to the endMarker)
transform.position = endMarker.position;
yield WaitForSeconds(waitTimeBetweenWaypoints);
time = 0;
// move object from endMarker to startMarker)
while ((transform.position != startMarker.position) && (time < speed))
{
time += Time.deltaTime;
lerpValue = (time / speed);
transform.position = Vector3.Lerp (endMarker.position, startMarker.position, lerpValue);
yield;
}
transform.position = startMarker.position;
yield WaitForSeconds(waitTimeBetweenWaypoints);
}
yield;
}
Here is a Unity package with a demo scene so you can see how it works.
Yes! Worked perfectly thank you! I did upvote the reply sorry for no feedback but worked amazing!
Do you know a script for making this same thing but with multiple waypoints? so the AI goes to set waypoints like a patrol route?
Sorry, just saw this question. This can definitely be done. Could you pleas post this in a separate question? You can make sure that I am notified by placing @$$anonymous$$avina in the question.
But before you do, might I suggest looking in the Asset store? Here are two good way point systems (written in C#) that are free to download
Your answer
Follow this Question
Related Questions
Game object position is stuck when using mathf.sin,Object's position stuck when using Math.sin 0 Answers
Tilting a camera around an object 0 Answers
Acces shader color and lerp 1 Answer
Help with recttransform lerp , lags on mobile not on pc. 1 Answer
Lerp to Gaze Location Smoothing Motion Issues (Cardboard) 0 Answers