- Home /
Trying to translate a game object a fixed distance
I have a game object I am trying to move a fixed distance over a fixed period of time. Unfortunately it seems that the only way to move things is to give it a vector and let it change over time. I must be missing something because right now I'm checking to see if the object has moved past its desired distance and if it has I set it to the correct distance and tell it to stop moving. It works, but it sure is ugly. I'm trying to do the same thing with rotation, any help would be appreciated (C#).
Been doing some reading, and I figure I have to user lerp, but for some reason the object moves instantaneously. I'm not quite sure how to make it move over time. Is this because I'm using it in update()?
Answer by aldonaletto · Aug 01, 2011 at 10:47 PM
If your object don't need to check for collisions while moving, the best way is using Lerp (or Slerp, for rotations) in a coroutine. Suppose you want the object to go from pointA to pointB in time seconds:
public var moving: boolean = false;
function MoveFromTo(pointA: Vector3, pointB: Vector3, time: float){
if (moving) return; // ignore other calls while moving
moving = true; // signals "I'm moving, don't bother me!"
var t: float = 0;
while (t < 1){
t += Time.deltaTime / time; // sweeps from 0 to 1 in time seconds
transform.position = Vector3.Lerp(pointA, pointB, t); // set position proportional to t
yield; // leave the routine and return here in the next frame
}
moving = false; // finished moving
}
This is a coroutine, so you can "fire and forget": just call the routine and go ahead - it will run without any intervention. If you need to know when it has finished, check the moving variable - it will be true while moving.
The same idea applies to rotations or vectors: just replace the line using Lerp to the equivalent using Slerp. If you like quaternions, use Quaternion.Slerp like below:
...
transform.rotation = Quaternion.Slerp(rotationA, rotationB, t);
...
where rotationA and rotationB are quaternions. But if you afraid these strange creatures and prefer vectors, you can do something like this:
...
transform.forward = Vector3.Slerp(oldDirection, newDirection, t);
...
what would make the object gradually turn its front to newDirection vector.
Better yet, you can move and rotate at the same time using the same function! Just use the two lines in the same loop:
...
transform.position = Vector3.Lerp(...);
transform.rotation = Quaternion.Slerp(...);
...
EDITED: Using coroutines in C# is somewhat tricky. To easy things, I'm posting a C# version below. I'm not a C# expert, so this script may have some stupid things (if any, let me know, C# experts) but it works!
public bool moving = false;
IEnumerator MoveFromTo(Vector3 pointA, Vector3 pointB, float time){
if (!moving){ // do nothing if already moving
moving = true; // signals "I'm moving, don't bother me!"
float t = 0f;
while (t < 1f){
t += Time.deltaTime / time; // sweeps from 0 to 1 in time seconds
transform.position = Vector3.Lerp(pointA, pointB, t); // set position proportional to t
yield return 0; // leave the routine and return here in the next frame
}
moving = false; // finished moving
}
}
You can't call coroutines directly in C# - you must use StartCoroutine:
StartCoroutine(MoveFromTo(pA, pB, time));
Thanks, this helps a lot. I just need to figure out how to take this and convert it to C# and make sense of it. Wish I wasn't a program$$anonymous$$g n00b...
I've posted a C# version in my answer (changing coroutines from Unityscript to C# is tricky!)
Hi, I know it's been a while since you've posted this, how would you go about making it so that you can fire another movement command while the object is still moving. for example if you press button twice, object is moved 2 fixed steps, or if you press once and second time while moving he would finish the first step and make another exact step.
Answer by DaveA · Aug 01, 2011 at 10:22 PM
http://unity3d.com/support/documentation/ScriptReference/Vector3.Lerp.html
Compute the start and end positions and see the examples there. It's similar for Quaternion (rotation)
That is what I'm using now, but it translates the object instantaneously and results in a jittering effect. From what I've read the lerp needs to be in a loop of sorts that iterates the starting position. Still trying to figure it out...
I see you have your answer, but if you were getting instantaneous translation, you didn't follow the example on that page, using deltaTime.
The examples on that page aren't very good. See here: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html
Answer by Eric5h5 · Aug 01, 2011 at 10:32 PM
Don't use Update. http://www.unifycommunity.com/wiki/index.php?title=MoveObject
Use this resource it contains all the types of translations, animations and tween effect and it works perfectly with no performance issues -> http://itween.pixelplacement.com/index.php
Your answer
Follow this Question
Related Questions
How to make a object translate alonge an other 2 Answers
How to make this kind of control ? 2 Answers
Moving a GameObject to a certain point in world space via script 1 Answer
help with gameObject.translate 1 Answer
C# Creating a 2d boundary 1 Answer