How would I make an object move from the position of object A, to the position of object B to the position of object C and then to A, etc. at a constant speed? I have tried looking into the "moving platform" script from the 2D platform tutorial from the unity resources but the speed seems to be based only on two objects, how can I have it be more (and at a constant speed)? Here is the script.
> var targetA : GameObject; var targetB
> : GameObject;
>
> var speed : float = 0.1;
>
> function FixedUpdate () {
> transform.position =
> targetA.transform.position * speed
> + targetB.transform.position * speed; }
and here is a modified version (mine) that, when applied to an object, appears to do nothing(I am getting no errors though).
var targetA : GameObject;
var targetB : GameObject;
var speed : float = 0.1;
function FixedUpdate () {
transform.position = targetA.transform.position * speed
+ targetB.transform.position * speed;
}
I made this code and didn't test it, but it should work or at least close to solve your problem. It's in JavaScript.
//you can use Vector3 arrays instead of Transform and that way you avoid the need of GameObjects like this:
/*
var waypoint : Vector3[];
and you can set waypoints relative to object position and
make different waypoints for any game object.. but that would need different coding :p
*/
var targetA : Transform;
var targetB : Transform;
var targetC : Transform;
private var currentTarget : Transform;
var proximity : float = 1.0;
var speed : float = 1.0;
//with function Start we set at start the current target
function Start ()
{
currentTarget = targetA;
}
function Update ()
{
var Distance : Vector3 = currentTarget.transform.position - transform.position;
//if "player" is "1" unit far, change currentTarget to next one
if(Distance.magnitude < proximity)
{
switch(currentTarget)
{
case targetA:
currentTarget = targetB;
break;
case targetB:
currentTarget = targetC;
break;
case targetC:
currentTarget = targetA;
break;
}
}
transform.position = Vector3.Slerp(transform.position, currentTarget.transform.position, Time.deltaTime * speed);
//make object look towards currentTarget
transform.LookAt(currentTarget);
}
Now if you want to move your object through more complex waypoints i recommend you using arrays... I leave you this link from a code of mine but be aware it has some motion bugs...