- Home /
GameObject rotates away from camera when using ScreenToWorldPoint
I got a simple 3D Game Object (A cube transformed in the shape of a rectangle) with an equally simple script to (in theory) make it look at the position the mouse is.
Now, the hierarchy I have is as follows:
Empty GameObject.
|- Object that should rotate.
| \- Script that handles the "lookAt"
\- Main Camera
The main camera is slightly above the object that should rotate, looking down.
The (important part of the) code is as follows:
public void Start() {
_mainCamera = Camera.main;
}
public void Update() {
Vector3 positionOnWorld = _mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, _mainCamera.nearClipPlane));
Vector3 lookingAt = new Vector3(positionOnWorld.x, 12f, positionOnWorld.z);
transform.LookAt(lookingAt);
}
Main camera's clipping planes are at 0.3 and 1000.
And when I run the scene, the Y rotation gets flipped (On the left, before running, on the right, while running):
Now, the weird part is that this issue seems to happen only with this particular object, as I created a different one following the same structure and it seems to go smooth. The problematic gameobject is not rotated in any way (and is not getting rotated anywhere) and the code works, as the "test" gameObject is using exactly the same lines.
Am I missing something here?
Is the parent being rotated in any way? Is the rotation shown in your picture of the gameobject or of the parent?
The parent is not rotated, and the rotation is for the gameObject inside the parent, yeah.