x


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()?

more ▼

asked Aug 01 '11 at 09:51 PM

Kartzan gravatar image

Kartzan
1 6 6 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.
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));
more ▼

answered Aug 01 '11 at 10:47 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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)
10|3000 characters needed characters left

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)

more ▼

answered Aug 01 '11 at 10:22 PM

DaveA gravatar image

DaveA
26.5k 151 171 256

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
Aug 01 '11 at 11:30 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Aug 01 '11 at 10:32 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2097
x355
x207
x33

asked: Aug 01 '11 at 09:51 PM

Seen: 3658 times

Last Updated: Aug 03 '11 at 02:57 AM