- Home /
Having trouble with LookRotation at mouse on one axis
I have a 2D game with a vehicle that moves left or right on the X-axis and a turret that points up a long the Y-axis. I tried writing my own script getting this turret to rotate left or right on the Z-axis to look at the mouse cursor, but it didn't quite work out as nothing is happening. The turret won't move at all. Here is my script:
public float rotateSpeed;
void FixedUpdate () {
Vector3 mousePos = Input.mousePosition;
Vector3 lookPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
lookPos.x = 0;
lookPos.y = 0;
Quaternion targetRotation = Quaternion.LookRotation(lookPos - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed);
}
}
I understand basically what LookRotation does but I think I'm plugging the wrong info into it. What am I doing wrong here?
Answer by robertbu · Oct 25, 2013 at 12:39 AM
Two issues here. For the calculation of ScreenToWorldPoint(), the 'z' parameter needs to be the distance in front of the camera. By default 'mousePosition' as a 'z' value of 0.0, so ScreenToWorldPoint() will return the position of the camera. Say your camera is at -10 on the 'z' axis and your 2D game has played a 'z' = 0.0. You would set 'mousePos.z = 10.0f' before you call ScreenToWorldPoint(). Again, you are setting the 'z' parameter to the positive distance in front of the camera to calculate the point.
The second issue is the axis that LookRotation uses as up. By default, it use Vector3.up, but since you are rotating around the 'z' axis, you need to use Vector.forward. Putting the two changes together:
public float rotateSpeed = 45.0;
void Update () {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10.0f;
Vector3 lookPos = Camera.mainCamera.ScreenToWorldPoint(mousePos);
Quaternion targetRotation = Quaternion.LookRotation(lookPos - transform.position, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed);
}
Note that this code assumes that you are aiming the forward of the game object this script it is attached to at the mouse position.
Thank you so much, you explained it perfectly. Now I understand how ScreenToWorldPoint works. The script works now but the turret isn't rotating how I need it to. I'm just going to post a screenshot ins$$anonymous$$d of trying to explain it. I penciled in the axes of the gameworld and the arrows represent which way I need the turret to rotate.
The second screen is what the turrets do when the script is applied.
The model I made in Blender where the up is Z and forward is Y but Unity converts that to Y up and Z forward. $$anonymous$$anipulating the Z rotation of the turret in Unity gets it to move in the direction I need it to. So what exactly do I have to do with this model to aim up, point at the mouse, and rotate correctly?
For your code to work, the barrel of the gun must point to positive 'z' when the rotation is (0,0,0). The other possible issue is that you did not correctly set the distance parameter. The distance in the ScreenToWorldPoint() needs to be the distance from the camera plane to the 2D plane you play your game on. Set it too far, and the gun rotation will not stay on the XY plane.
I believe my set the distance correctly. The camera is set to -31 on Z-axis and I set it to 31.0f in the script.
I think the issue lies with my model. I'll mess around with that I think I might understand what's going on. But you have helped me fix and understand the script so thank you very much!
Your answer

Follow this Question
Related Questions
Can't use camera.main [SOLVED] 1 Answer
How to make a mesh into a FirstPerson Character? 2 Answers
What am I doing wrong with these rotations? 0 Answers
transform.right = (point on axis) after using LookAt? 2 Answers
Rotating about and only one axis 1 Answer