Hello everyone
I guess I found an ugly bug in Unity 3D. Imagine 2 objects in the scene:
a game grid (Unity Plane)
a cube (Unity Cube) outside the grid
There is now a routine on the Plane getting the mouse position and then calling a move routine attachted to the cube. This routine will now move the cube to the target position on the grid where the mouse was clicked.
The routine itself works flawless and looks like this (Javascript):
Now the weird behaviour of Unity 3D: Depending on what I have clicked (focussed) in the scene view before starting the game the “move done” after moving to targetPos will be printed sooner or later.
Nothing focussed in Scene view: “print move” will never be displayed!
Plane focussed in Scene view: “print move” will be displayed after 1 - 5 seconds (always different)
Cube focussed in Scene view: “print move” will be displayed immediately after moving to target position
You get the same behaviour changing the focus while the game is running like this:
Make sure nothing is focussed in Scene view (click anywhere in the grey)
Start game
Move the cube by clicking anywhere to grid → “move down” will not be displayed because nothing was focussed in Scene view
Now focus the moved cube in Scene view and tadaaaaa: “Move done” will be displayed immediately
Before you telling me: Dude, use the Update() function to do moves then I have to tell you: I did and got the same behaviour as described!!! So there is a general bug on Unity 3D tracking objects…
Building it and let it run is like having focussed nothing (“move done” will never be displayed).
My config:
Mac Powerbook with OSX Leopard (not Snow Leopard)
Unity 3D Indie 2.6 (got the same error with Unity 3D Indie 2.5)
This behaviour is really bad to my project, not to say: a killer criteria for my project.
I have no idea why Unity 3D can’t handle such a simple task. :(((((
The problem isn’t Unity, the problem is your code. For one thing, “transform.position.z != targetPos.z transform.position.z != targetPos.z” doesn’t make much sense, since that’s the same thing repeated twice. For another thing, comparing two floats like that is a bad idea, because lots of things will cause the two to not be 100% precisely equal, and thus the loop will never end. Using “Vector3.Lerp (transform.position, newPos, Time.deltaTime*40);” doesn’t make any sense either, because the third value in Lerp is supposed to be a value from 0 to 1 (0 returns the first parameter, 1 returns the second), and Time.deltaTime tells you the time difference between the current frame and the previous frame. So just do a lerp from 0 to 1 over time instead.
First of all: tnx for the quick answer!
Lerp/While doubler effect: I know what you mean with the doubler effect. The problem with WHILE is the fact the routine shall end IF the cube has reached the target position! How else could I solve it? If I dont use the LERP I don’t see the cube moving I have to click the mouse button several times to make the cube moving to its target position.
Twice coords check in WHILE: You’re right, copy/paste error, sould have been once: transform.position.z = … targetPos.z … but doesnt solve my general problem.
Third Lerp parameter: Ok, but if I dont use Time.deltaTime (only a fixed integer) the cube will be set to its target w/o any move in between, thats not what I want.
Lerp from 0 to 1: I don’t understand… What do you mean by that? Could you please give me a simple example?
Tnx again, Eric!
Erm, do I see it right, that your routine is time driven and not position driven? Imagine I’m working on a kinda 2D boardgame. There are many cubes on the gameboard. If the player wants to move a cube between two other cubes (already on the gameboard) the cube to be moved must be EXACTLY moved between the two other cubes! I guess thats not possible working time driven!
Sorry not explaining this closer before but thats my main problem.
Nope. You take the initial position, and the final position, and Lerp between the two, where 0 = starting position, and 1 = ending position. The example I gave would take 1 second to complete (if there was a yield in there, anyway). You can of course modify that to go as fast or as slow as you like.
var t = 0.0;
while (t < .5) {
t += Time.deltaTime;
transform.position = Vector3.Lerp (transform.position, newPos, t);
yield;
}
The yield is still necessary tough, else the cube is moved directly to its target position. Furthermore I realize I have to set the time (t) big enough to give the cube time to move around. I put it once to .1 and it stopped half way.
Ok, all over it works way better than it did before!
Thank you very much, excellent support!
Tyger