smooth mouse look script

Okay I’m making an FPS shooter type game. awhile ago I found these two mouse look scripts earlier I need help fixing (and this is the first time I’ve really tried to script in C# opposed to java).One other thing I’d like to do although I am willing to use the the script twice on a child object is to make the script rotate the parent object’s x axis while the child objects (put in a variable) y axis but I am willing to do otherwise :

this script works more like I want it to and I can easily modify one of the variables to block off x rotation but I cannot set my own clamp to the y axis

using UnityEngine;

using System.Collections;

 

// Very simple smooth mouselook modifier for the MainCamera in Unity by FatiguedArtist 

//(Francis R. Griffiths-Keam) - www.fatiguedartist.com 

 

[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]

public class SimpleSmoothMouseLook : MonoBehaviour {

 

    public bool lockCursor = false;

 

    public float sensitivity = 30;

    public int smoothing = 10;

 

    float ymove;

    float xmove;

 

    int iteration = 0;

 

    float xaggregate = 0;

    float yaggregate = 0;

 

    //int Ylimit = 0;

    public int Xlimit = 20;

 

    void Start()

    {

        // make the cursor invisible and locked?

 

        if (lockCursor)

        {

            Screen.lockCursor = true;

        }

    }

 

 

    void FixedUpdate () {

 

        // ensure mouseclicks do not effect the screenlock

 

        if (lockCursor)

        if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))

        {

            Screen.lockCursor = true;

        }

 

        float[] x = new float[smoothing];

        float[] y = new float[smoothing];

 

        // reset the aggregate move values

 

        xaggregate = 0;

        yaggregate = 0;

 

        // receive the mouse inputs

 

        ymove = Input.GetAxis("Mouse Y");

        xmove = Input.GetAxis("Mouse X");

 

        // cycle through the float arrays and lop off the oldest value, replacing with the latest

 

        y[iteration % smoothing] = ymove;

        x[iteration % smoothing] = xmove;

 

        iteration++;

 

        // determine the aggregates and implement sensitivity

 

        foreach (float xmov in x)

        {

            xaggregate += xmov;

        }

 

        xaggregate = xaggregate / smoothing * sensitivity;

 

        foreach (float ymov in y)

        {

            yaggregate += ymov;

        }

 

        yaggregate = yaggregate / smoothing * sensitivity;

 

        // turn the x start orientation to non-zero for clamp

 

        Vector3 newOrientation = transform.eulerAngles + new Vector3(-yaggregate, xaggregate, 0);

 

  

       // xclamp = Mathf.Clamp(newOrientation.x, Xlimit, 360-Xlimit)%360;

 

        // rotate the object based on axis input (note the negative y axis)

        

        transform.eulerAngles = newOrientation;

        

    }

}

this script clamping rotation works fine but I can’t click any buttons on the mouse or the script won’t work any more, and while this script works smoothly it drags a little and does not stop when I no longer move the mouse.

using UnityEngine;

using System.Collections;

using System.Collections.Generic;



public class SmoothMouseLook : MonoBehaviour

{

    /*

    This script is used to average the mouse input over x 

    amount of frames in order to create a smooth mouselook.

    */



    //Mouse look sensitivity

    public float sensitivityX = 2f;

    public float sensitivityY = 2f;



    //Default mouse sensitivity

    public float defaultSensX = 2f;

    public float defaultSensY = 2f;



    //Minimum angle you can look up

    public float minimumY = -60f;

    public float maximumY = 60f;



    //Number of frames to be averaged, used for smoothing mouselook

    public int frameCounterX = 35;

    public int frameCounterY = 35;



    //Mouse rotation input

    private float rotationX = 0f;

    private float rotationY = 0f;



    //Used to calculate the rotation of this object

    private Quaternion xQuaternion;

    private Quaternion yQuaternion;

    private Quaternion originalRotation;



    //Array of rotations to be averaged

    private List<float> rotArrayX = new List<float> ();

    private List<float> rotArrayY = new List<float> ();



    void Start ()

    {

       //Lock/Hide cursor

       Screen.lockCursor = true;



       if (rigidbody)

         rigidbody.freezeRotation = true;



       originalRotation = transform.localRotation;

    }



    void Update ()

    {

       if (Screen.lockCursor) {

         //Mouse/Camera Movement Smoothing:   

         //Average rotationX for smooth mouselook

         float rotAverageX = 0f;

         rotationX += Input.GetAxis ("Mouse X") * sensitivityX;



         //Add the current rotation to the array, at the last position

         rotArrayX.Add (rotationX);



         //Reached max number of steps?  Remove the oldest rotation from the array

         if (rotArrayX.Count >= frameCounterX) {

          rotArrayX.RemoveAt (0);

         }



         //Add all of these rotations together

         for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {

          //Loop through the array

          rotAverageX += rotArrayX[i_counterX];

         }



         //Now divide by the number of rotations by the number of elements to get the average

         rotAverageX /= rotArrayX.Count;



         //Average rotationY, same process as above

         float rotAverageY = 0;

         rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;

         rotationY = ClampAngle (rotationY, minimumY, maximumY);

         rotArrayY.Add (rotationY);



         if (rotArrayY.Count >= frameCounterY) {

          rotArrayY.RemoveAt (0);

         }



         for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {

          rotAverageY += rotArrayY[i_counterY];

         }



         rotAverageY /= rotArrayY.Count;



         //Apply and rotate this object

         xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);

         yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);

         transform.localRotation = originalRotation * xQuaternion * yQuaternion;

       }

    }



    private float ClampAngle (float angle, float min, float max)

    {

       if (angle < -360f)

         angle += 360f;

       if (angle > 360f)

         angle -= 360f;



       return Mathf.Clamp (angle, min, max);

    }
    }

I have to apologise, as that first script is an old one of mine I posted when I was still learning. I would not trust that it works entirely as intended, though this locking issue does baffle me. I’ll test my old code and see if I can find what’s wrong.

If you are trying to perform a mouse-look script, you don’t need to have two instances of the same script if you don’t want to. The main thing to do is use two separate Quaternion operations, one for local-x and one for world-y.

Using Unity’s C# assembly, you can affect one Quaternion by another by using the * operator.

Edit:
There’s an updated version available here

If you look at the way the default Unity First Person Controller is put together you can see that they actually use the MouseLook Script in two places. The one on the root of the controller controls the horizontal rotation and the one on the camera itself controls the vertical rotation. This is actually really important to getting accurate rotations. I would recommend following that model and splitting your script to handle the X and Y separately.

As to the cursor drifting after you stop moving the mouse, that is to be expected with the way you implemented the smoothing. The amount of drift is going to be tied directly to the number of frames you keep in your buffer which you set here:

 //Number of frames to be averaged, used for smoothing mouselook
    public int frameCounterX = 35;
    public int frameCounterY = 35;

If you lower that number you will have less aggressive mouse smoothing and as well as less cursor drift. The trick is adjusting it to feel right.

I didn’t try running this script so I can’t say for sure, but I am not seeing anything here that would cause problems when you hit a mouse button. I would look for possible interactions from other scripts/components.

Probably not the easy answer you were looking for but I hope it’s of some use.