I’m currently working on an fps of sorts and have been trying to perfect my mouse look script. I want to make sure that the movement doesn’t feel jittery or sluggish, and I’ve written a pretty good script that works fairly well, however it’s still not as smooth as I’d like it to be. It also does this peculiar thing where if I move the mouse along the x axis, the view will bounce back a little bit from where the movement ends, giving a slight recoil effect which I’m not a fan of. I’d like some suggestions on how I could improve the following code:
public class PlayerLook : MonoBehaviour
{
public float mouseSensitivity = 100.0F;
public float clampAngle = 80.0F;
private float mouseX;
private float mouseY;
private float rotationX = 0.0F;
private float rotationY = 0.0F;
void Start()
{
Vector3 rotation = transform.localRotation.eulerAngles;
rotationX = rotation.x;
rotationY = rotation.y;
}
void Update()
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
rotationX += mouseY * mouseSensitivity * Time.deltaTime;
rotationY += mouseX * mouseSensitivity * Time.deltaTime;
rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
// Am using this line to smooth out the movement, but it produces recoil effects along the X axis.
transform.rotation = Quaternion.Lerp(transform.rotation, localRotation, 12f * Time.deltaTime);
}
}
My bad. It seems it is not that code that produces the recoil effect, rather it occurs as a result of some similar code which I have attached to a cameraReference object, which rotates with the camera to ensure that the player follows the camera only along a horizontal axis. This is the code I made for that object:
public class CameraReferenceLook : MonoBehaviour
{
public float mouseSensitivity = 100.0F;
private float mouseX;
private float rotationY = 0.0F;
// Start is called before the first frame update
void Start()
{
Vector3 rotation = transform.rotation.eulerAngles;
rotationY = rotation.y;
}
// Update is called once per frame
void Update()
{
mouseX = Input.GetAxis("Mouse X");
rotationY += mouseX * mouseSensitivity * Time.deltaTime;
Quaternion localRotation = Quaternion.Euler(0.0f, rotationY, 0.0f);
transform.rotation = localRotation;
}
}
I’m trying to figure out a better way to keep the player following the camera direction only along a horizontal axis. If I tell the player code that it’s forward vector is equal to the actual camera’s forward vector, my player flies upward if I aim the camera up. I use this cameraReference object instead, which can only rotate around the y axis, but it seems to interfere with my mouse-look code.
Alright, I found a solution to the cameraReference problem. I was able to get the player to move in the camera’s direction without following it upward or downward by inserting this blurb of code into my movement script:
var CharacterRotation = camera.transform.rotation;
CharacterRotation.x = 0;
CharacterRotation.z = 0;
transform.rotation = CharacterRotation;
With this code I was able to do away with the cameraReference object entirely.
I was then able to fix the wobbly recoil effect by changing the last line in my PlayerLook Code to the following:
transform.rotation = Quaternion.Lerp(transform.rotation, localRotation, 2f * Time.fixedTime);
I’m not exactly sure why this fixed my problem, but there it is. I still think this mouse-look could benefit from some better smoothing, so I’m still open to suggestions.