- Home /
Move from point to point?
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; }
Thanks :)
Answer by BLF-Games · Nov 24, 2010 at 10:29 AM
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...
http://answers.unity3d.com/questions/28390/enemy-jumping-with-each-waypoint-start
Thank you. I get an error when I am using the code
"Assets/NewBehaviourScript.js(43,38): BCE0017: The best overload for the method 'UnityEngine.Vector3.Slerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Transform, float)'."
Ah yes, try changing in that line currentTarget to currentTarget.transform.position
Is there any way to do somethings like this : gameObject.transform.$$anonymous$$oveTo(Point : transform,speed : float)
Your answer
Follow this Question
Related Questions
character controller sinks into platform while using raycast to detect collision 0 Answers
Moving platform and character controller 0 Answers
Creating a 2.5D Platformer.. use rigidbody/character controller, or custom code? 0 Answers
jittery collisions 2d platformer 1 Answer
Can't change direction mid-jump 1 Answer