|
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()?
(comments are locked)
|
|
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:
private 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. ... 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. ... 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 programming n00b...
Aug 01 '11 at 10:51 PM
Kartzan
I've posted a C# version in my answer (changing coroutines from Unityscript to C# is tricky!)
Aug 02 '11 at 02:28 AM
aldonaletto
(comments are locked)
|
|
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...
Aug 01 '11 at 10:42 PM
Kartzan
I see you have your answer, but if you were getting instantaneous translation, you didn't follow the example on that page, using deltaTime.
Aug 01 '11 at 10:52 PM
DaveA
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
Aug 01 '11 at 11:30 PM
Eric5h5
(comments are locked)
|
|
Don't use Update. http://www.unifycommunity.com/wiki/index.php?title=MoveObject
(comments are locked)
|
