Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help me make this mouse-look script better.

Discussion in 'Scripting' started by TheDuples, Jan 25, 2019.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    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:

    Code (CSharp):
    1. public class PlayerLook : MonoBehaviour
    2. {
    3.     public float mouseSensitivity = 100.0F;
    4.     public float clampAngle = 80.0F;
    5.  
    6.     private float mouseX;
    7.     private float mouseY;
    8.  
    9.     private float rotationX = 0.0F;
    10.     private float rotationY = 0.0F;
    11.  
    12.     void Start()
    13.     {
    14.         Vector3 rotation = transform.localRotation.eulerAngles;
    15.         rotationX = rotation.x;
    16.         rotationY = rotation.y;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         mouseX = Input.GetAxis("Mouse X");
    22.         mouseY = Input.GetAxis("Mouse Y");
    23.  
    24.         rotationX += mouseY * mouseSensitivity * Time.deltaTime;
    25.         rotationY += mouseX * mouseSensitivity * Time.deltaTime;    
    26.  
    27.         rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
    28.  
    29.         Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
    30.  
    31.         // Am using this line to smooth out the movement, but it produces recoil effects along the X axis.
    32.         transform.rotation = Quaternion.Lerp(transform.rotation, localRotation, 12f * Time.deltaTime);
    33.     }
    34. }
     
  2. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    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:

    Code (CSharp):
    1. public class CameraReferenceLook : MonoBehaviour
    2. {
    3.     public float mouseSensitivity = 100.0F;
    4.  
    5.     private float mouseX;
    6.  
    7.     private float rotationY = 0.0F;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         Vector3 rotation = transform.rotation.eulerAngles;
    13.         rotationY = rotation.y;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         mouseX = Input.GetAxis("Mouse X");
    20.  
    21.         rotationY += mouseX * mouseSensitivity * Time.deltaTime;
    22.  
    23.         Quaternion localRotation = Quaternion.Euler(0.0f, rotationY, 0.0f);
    24.         transform.rotation = localRotation;
    25.     }
    26. }
    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.
     
  3. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    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:

    Code (CSharp):
    1. var CharacterRotation = camera.transform.rotation;
    2.         CharacterRotation.x = 0;
    3.         CharacterRotation.z = 0;
    4.  
    5.         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:

    Code (CSharp):
    1. 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.