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).
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?
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
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!
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.