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

Limiting angle to look up and down in Unity mouselook.cs script

Discussion in 'Scripting' started by Larpushka, Nov 24, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Hi guys, I'm trying to find the modifier that limits the angle to look up and down in the Unity mouselook.cs script... can anyone help me find it?
    If it doesn't exist I'm planning to create it, I just wanted to see if I missed anything

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. namespace UnityStandardAssets.Characters.FirstPerson
    6. {
    7.     [Serializable]
    8.     public class MouseLook
    9.     {
    10.         public float XSensitivity = 2f;
    11.         public float YSensitivity = 2f;
    12.         public bool clampVerticalRotation = true;
    13.         public float MinimumX = -70F;
    14.         public float MaximumX = 70F;
    15.         public bool smooth;
    16.         public float smoothTime = 5f;
    17.  
    18.  
    19.         private Quaternion m_CharacterTargetRot;
    20.         private Quaternion m_CameraTargetRot;
    21.  
    22.  
    23.         public void Init(Transform character, Transform camera)
    24.         {
    25.             m_CharacterTargetRot = character.localRotation;
    26.             m_CameraTargetRot = camera.localRotation;
    27.         }
    28.  
    29.  
    30.         public void LookRotation(Transform character, Transform camera)
    31.         {
    32.             float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
    33.             float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
    34.  
    35.             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
    36.             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
    37.  
    38.             if(clampVerticalRotation)
    39.                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
    40.  
    41.             if(smooth)
    42.             {
    43.                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
    44.                     smoothTime * Time.deltaTime);
    45.                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
    46.                     smoothTime * Time.deltaTime);
    47.             }
    48.             else
    49.             {
    50.                 character.localRotation = m_CharacterTargetRot;
    51.                 camera.localRotation = m_CameraTargetRot;
    52.             }
    53.         }
    54.  
    55.  
    56.         Quaternion ClampRotationAroundXAxis(Quaternion q)
    57.         {
    58.             q.x /= q.w;
    59.             q.y /= q.w;
    60.             q.z /= q.w;
    61.             q.w = 1.0f;
    62.  
    63.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
    64.  
    65.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
    66.  
    67.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
    68.  
    69.             return q;
    70.         }
    71.  
    72.     }
    73. }
    74.  
     
  2. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Nevermind, got it!