Well I’ve been working on Translate gizmo at runtime, so you could click on object, 3 arrow gizmo would appear and you could drag the arrows to translate the object. So far everything is fine, but I just noticed this annoying problem that I can’t solve. Basically, when I’m standing and facing positive X axis and I try to use the gizmo to translate an object right/left (change the X position of it) it works fine. It works fine until i move onto the other side facing -X axis, then for some reason it becomes inverted and when I drag the arrow right it moves the object left instead of right etc.
Can’t figure out how to fix this :S
Here’s the code I’m using for the translation.
void OnMouseDrag() {
float delta;
if (mouseDown) {
delta = Input.GetAxis("Mouse X") * Time.deltaTime;
delta *= moveSensitivity;
otherTrans.Translate(Vector3.right * delta);
}
}
Actually I didn’t say it right, more of it inverts if I move to other side of the object. E.g. if I’m on one side of the object it works fine, if I’m on the other side of object, the translation becomes inverted.
What am I doing wrong or what did I forget? :S
Thanks
Same thing happens on this demo of runtime translate/rotate/scale gizmos that I’ve found: http://dl.dropbox.com/u/15234/answers-unity/Gizmo/Gizmo.html
Try to translate the object along the X axis from one side and then move to other side and try to do the same, you’ll see what I mean then.
The translation will be inverted :S
I understand that ScreenToWorldPoint has to be used but can’t figure out how…
Try this
delta.x = Input.GetAxis("Mouse X") * Time.deltaTime;
delta.y = 0.0f;
delta.z = 0.0f;
delta.x *= selected.collider.bounds.size.x;
delta = Camera.main.cameraToWorldMatrix.MultiplyVector(delta);
selected.transform.Translate(delta, Space.World);
To make it work like in the Editor ie only alter the local x axis do it this way
delta = Vector3.zero;
delta.x = Input.GetAxis("Mouse X") * selected.collider.bounds.size.x;
delta = Camera.main.cameraToWorldMatrix.MultiplyVector(delta);
delta.y = 0.0f;
delta.z = 0.0f;
selected.transform.Translate(delta);
Some exposition: your original code applies a screen-space delta to world-space without accounting for how the screen (view) is oriented. Moving your mouse right always results in a +x delta. You need to calculate how +x in mouse coordinates relates to world-space – i.e. account for the rotation of the camera. That’s what SpookyCat’s use of cameraToWorldMatrix takes care of.
Thanks a bunch, works fine for X axis! I wonder why doesn’t it work for Z axis though… :S
Well, mice don’t move in z…
You’ll have to provide more detail on what’s (not) happening…
Nothing’s happening, same thing as before 
It inverts just like it did before, the SpookyCat’s code which fixed X axis doesn’t seem to work with Z :S.
Here’s my code:
delta = Vector3.zero;
delta.z = (Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y")) * otherTrans.collider.bounds.size.z;
delta = cam.cameraToWorldMatrix.MultiplyVector(delta);
delta.y = 0.0f;
delta.x = 0.0f;
otherTrans.transform.Translate(delta, Space.World);
I tried like what ever I could think of but it inverts one way or another lol.
Ok try this.
X Axis
// Moving the x axis
Vector3 delta = new Vector3();
Vector3 right = Camera.main.transform.TransformDirection(Vector3.up);
delta = Vector3.zero;
delta.x = (Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y")) * selected.collider.bounds.size.x;
delta = Camera.main.cameraToWorldMatrix.MultiplyVector(delta);
if ( right.y < 0.0f )
delta.x = -delta.x;
delta.z = 0.0f;
delta.y = 0.0f;
selected.transform.Translate(delta, Space.World);
Z Axis
// Moving the z axis
Vector3 right = Camera.main.transform.TransformDirection(Vector3.up);
delta = Vector3.zero;
delta.x = (Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y")) * selected.collider.bounds.size.z;
delta = Camera.main.cameraToWorldMatrix.MultiplyVector(delta);
if ( right.y < 0.0f )
delta.z = -delta.z;
delta.y = 0.0f;
delta.x = 0.0f;
selected.transform.Translate(delta, Space.World);
Nop, the Z still inverts…
You sure you’re not getting confused between Y and Z axis? The code you posted above translates to Y axis (up and down). I changed delta.y to delta.z but it doesn’t work, still inverts like it did before :S
Arr yes could have something to do with the fact all my objects come from max and hence have a x rotation of 270, made the changes above to make it work for you finally 
Yay works, thanks a bunch :))