Constant Speed?

Ive been struggling with this for a week, im trying to move between random locations all with a constant speed.

target = Vector3(Random.Range(-20, 50),Random.Range(1, 20),Random.Range(-20, 50));
var mySpeed = Vector3.Distance(transform.position, target);

function Update() {
 transform.position = Vector3.Lerp(transform.position, target, mySpeed);
}

this code doesnt appear to move my object at all, any ideas?

Try:

target = Vector3(Random.Range(-20, 50),Random.Range(1, 20),Random.Range(-20, 50));
var mySpeed = 1.0 / Vector3.Distance(transform.position, target);

function Update() {
 transform.position = Vector3.Lerp(transform.position, target, mySpeed * Time.deltaTime);
}

hmm, its moving quite fast, is there a way to slow it down?

Yeah, put in another variable for speed, something between 0 and 1:

transform.position = Vector3.Lerp(transform.position, target, mySpeed * myOtherSpeed * Time.deltaTime);

ive put in the variable, still too fast, any ideas?

Don’t use Update; coroutines are much easier for this sort of thing. If you do a search you can find a bunch of topics about this.

–Eric

Si, Ive put it in a coroutine, but it just appears to crash unity, any ideas?

function Start() {
	
	move();
}


function move() {
    while (true) {
    	transform.position = Vector3.Lerp(transform.position, target, mySpeed * myOtherSpeed * Time.deltaTime);
    }
    
    
}

Your current code goes into an infinite loop, which stops Unity from responding.

Your while loop just loops on and on without stopping at one point to give the engine a chance to go to the next frame. You have to add a “yield;” statement to your while loop which will exit the coroutine, give the engine a chance to advance to next frame and continue the coroutine (and so on).

so ive added the following but the game object doesnt move at all?

function move() {
    while (true) {
    	transform.position = Vector3.Lerp(transform.position, target, mySpeed * myOtherSpeed * Time.deltaTime);
    	yield;
    }

Check that target is set and that (mySpeed * myOtherSpeed * Time.deltaTime) is larger than 0.

If it’s 0, then Lerp just returns the start vector, which would keep the object at it’s position.

It does return 0. It defiantly is making a target different from the current position of the game object. When I remove the var myOtherSpeed it moves the game object but at a speed that is too fast… what can I do to make this work?

anyone? really stuck… thanks!

myOtherSpeed is clearly 0. So, you need to make myOtherSpeed have whateer its correct value is supposed to be.

I’m afraid to ask, but what IS your value for myOtherSpeed?
Did you try 0.00001? Is that still too fast?

Also, when I use this (w/out myOtherSpeed) it’s going way too slow for my taste. Are you using especially small objects viewed close up? Or are you expecting it to take an hour or so to get to where it’s going?

I think something besides the code is the problem, in other words.

Make a new empty project. Stick a cube at 0, 0, 0 and throw this script on it:

var target;
var mySpeed;

function Start() {
	target = Vector3(Random.Range(-50, 50),Random.Range(-50, 50),Random.Range(-5, 5)); 
	mySpeed = 1.0 / Vector3.Distance(transform.position, target); 
	Move();
}

function Move() {
	while (true) { 
		 transform.position = Vector3.Lerp(transform.position, target, mySpeed * Time.deltaTime); 
		 yield;
	}
}

See if you’re getting the same crawling motion I am :slight_smile:

Yep I tried a low number like 0000000.1…

Just created a new project and a cube, I get the error:

Operator ‘*’ cannot be used with a left hand side of type ‘object’ and a right hand side of type float…

I should also make it clear that im using iPhone unity

Maybe that was a typo but:
0000000.1 is 0.1
That is not that low
0.00001 is what is needed. (notice the decimal point placement)
Again, probably a typo, but specifics count when you’re coding! :smile:

For the code, did you copy and paste into a new javascript? If so, that’s pretty odd, because it works without a hitch. I even just copied and pasted right from the post like you would have to make sure nothing got wonky in the transition. Maybe you created a C# script? (unlikely, I know.)

I don’t know how to help you if legitimate working scripts give you syntax errors.

Edit: Okay, so… Unity iPhone. That doesn’t really explain things, though. Anyone with Unity iPhone want to chime in here? I really can’t help at this point, anymore. Sorry.

Edit: Actually, that does explain it just fine. See below.

Doesn’t the iPhone force “#pragma strict” (no dynamic typing)?

In this case, you need to declare the type of mySpeed (I’d do that always anyway - helps to catch errors early). Since you don’t, it’s assumed to be the lowest common type, object. And then it doesn’t know how to multiply an object with a float (Time.deltaTime).

In regular Unity it then figures out at runtime that object is actually a float and is able to multiply them. The iPhone doesn’t support this runtime-detection.

That would be it. Thanks! C# is my usual language so I missed that.

So, this oughta do it:

var target : Vector3; 
var mySpeed : float; 

function Start() { 
   target = Vector3(Random.Range(-50, 50),Random.Range(-50, 50),Random.Range(-5, 5)); 
   mySpeed = 1.0 / Vector3.Distance(transform.position, target); 
   Move(); 
} 

function Move() { 
   while (true) { 
       transform.position = Vector3.Lerp(transform.position, target, mySpeed * Time.deltaTime); 
       yield; 
   } 
}