It’s because mousePosition gives you the position in screen space. Also, when you use LookAt(), the camera turns towards a transform in world space which has those coordinates. The value returned by mousePosition is also in pixels, which means those coordinates will be something like (200,300,0).
When you move your mouse, that value stays always in that same XY plane, and that’s why the camera will not move horizontally.
If you want to use LookAt, you need a target relative to your camera position. One lazy way to do it is:
var c = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.LookAt(c);
But that’s crude. If you’re trying to do an FPS game, then here’s
How:
I recommend: Simply use the FPSController component included in the standard assets. (Assets > Import Package > Characters )
You need to cast a ray from the camera which hits at a position. From there you need to calculate the looking direction. Contraint z, so that the lookRotation is only applied to XY.
something like this:
float speed = 4f;
RaycastHit hit;
Ray ray;
Quaternion targetRotation;
void Update() {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, Camera.main.transform.forward, out hit, 1000.0f)){
targetRotation = Quaternion.LookRotation(hit.point - transform.position);
targetRotation.z = 0f;
}
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}