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. Dismiss Notice

CS GO like mouselook?

Discussion in 'Scripting' started by IAMBATMAN, Sep 20, 2016.

  1. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    If you've ever played CS GO (I haven't played the other ones) then you'll probably have noticed how good the mouselook feels, it's very smooth and responsive. I want to do something like this in unity, but I'm not exactly sure how.

    Do you have any tips or know about an already made CSGO mouse look script?
     
  2. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    There is an FPS controller in the standard assets, its pretty smooth and pretty easy to customize.
     
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    I've looked at a few different smoothing options, but not found any that are perfect the way CS: Source is.

    There is an averaged version here - http://forum.unity3d.com/threads/really-need-some-help-with-mouselook-s.49243/#post-391491

    Code (csharp):
    1.     public float _mlTwoSensitivityX = 2.5f;
    2.     public float _mlTwoSensitivityY = 2.5f;
    3.     public List<float> _rotArrayX = new List<float>();
    4.     public List<float> _rotArrayY = new List<float>();
    5.     public int _averageFromThisManySteps = 3;
    6.  
    7.     void Update()
    8.     {
    9.         MouseLook();
    10.     }
    11.  
    12.     void MouseLook()
    13.     {
    14.         float rotAverageX = 0f;
    15.         float rotAverageY = 0f;
    16.  
    17.         float rotX = 0f;
    18.         float rotY = 0f;
    19.  
    20.         rotX += Input.GetAxis("Mouse X") * _mlTwoSensitivityX;
    21.         rotY += Input.GetAxis("Mouse Y") * _mlTwoSensitivityY;
    22.  
    23.         // Add current rot to array, at end
    24.         _rotArrayX.Add(rotX);
    25.         _rotArrayY.Add(rotY);
    26.  
    27.         // Reach max number of steps? Remove oldest
    28.         if (_rotArrayX.Count >= _averageFromThisManySteps)
    29.             _rotArrayX.RemoveAt(0);
    30.  
    31.         if (_rotArrayY.Count >= _averageFromThisManySteps)
    32.             _rotArrayY.RemoveAt(0);
    33.  
    34.         //Add all of these rotations together
    35.         for (int i_counterX = 0; i_counterX < _rotArrayX.Count; i_counterX++)
    36.         {
    37.             rotAverageX += _rotArrayX[i_counterX];
    38.         }
    39.  
    40.         for (int i_counterY = 0; i_counterY < _rotArrayY.Count; i_counterY++)
    41.         {
    42.             rotAverageY += _rotArrayY[i_counterY];
    43.         }
    44.  
    45.         // Get average
    46.         rotAverageX /= _rotArrayX.Count;
    47.         rotAverageY /= _rotArrayY.Count;
    48.  
    49.         // Apply
    50.         transform.Rotate(0f, rotAverageX, 0f, Space.World);
    51.         transform.Rotate(-rotAverageY, 0f, 0f, Space.Self);
    52.     }
    Unity also has issues with Windows 7 where the input lag from Aero will affect things. You can get rid of the lag by running the exe in compatability mode with "Disable desktop composition/visual themes" checked.
     
    IAMBATMAN likes this.
  4. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    Thanks, Stardog! I'm testing it now. Will I have to tell my windows 7 players to check "Disable desktop composition/visual themes"? I don't have a computer running windows 7, how much does it affect them?
     
    Last edited: Sep 20, 2016
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    For me it affected things a lot. It can make 60 fps not "feel" like 60. It's possible that it's only an issue when running windowed. I'm making an MP shooter targetting 120 fps and had to deal with this.

    For players, you could include a shortcut that is already setup that way by default. Maybe get someone who has W7 to make the shortcut for you.

    Also, you might want to look at the source code of games like Doom/Quake and look for the mouse look code.
     
  6. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    Alright, thanks for the help. The script is great :)