Search Unity

Understanding Clamping Rotation

Discussion in 'Scripting' started by Mergster, Dec 6, 2017.

  1. Mergster

    Mergster

    Joined:
    Dec 8, 2016
    Posts:
    10
    Hello! I'm trying to clamp only my x rotation between -90 and 90 degrees using:

    private float xRotation = 0.0f;

    void Update()
    {
    xRotation = Mathf.Clamp(xRotation, -90, 90);
    transform.eulerAngles = new Vector3(xRotation, 0.0f, 0.0f);
    }


    However, the result of this is that my character can only rotate -1 to 1 on all axis.
    I don't understand why this is working like that.


    Iv'e been looking through all code samples and trying things for 2 hours now. :(
    Any help is very much appreciated :)
     
  2. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Your code is very confusing..You are setting your xRotation to = 0... Then you are saying that your xRotation is = the Constraints of itself, -90, 90... Here is a code i have for my game.. Its a touch to rotate game.. Using ray casts and nav mesh i move my player so he doesn't rotate by touch, only by the given path, but this is attached to my camera and i can rotate my camera based on the min and max values i set it too.. Min and max being up and down and able to turn a full 360.. You can hopefully use it and change it to your needs..

    GoodLuck.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
    7. public class InputManager : MonoBehaviour
    8. {
    9.  
    10.     public Transform target;
    11.     public float distance = 5.0f;
    12.     public float xSpeed = 120.0f;
    13.     public float ySpeed = 120.0f;
    14.     public float yMinLimit = -20f;
    15.     public float yMaxLimit = 80f;
    16.     public float distanceMin = .5f;
    17.     public float distanceMax = 15f;
    18.     public float smoothTime = 2f;
    19.     float rotationYAxis = 0.0f;
    20.     float rotationXAxis = 0.0f;
    21.     float velocityX = 0.0f;
    22.     float velocityY = 0.0f;
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         Vector3 angles = transform.eulerAngles;
    27.         rotationYAxis = angles.y;
    28.         rotationXAxis = angles.x;
    29.         // Make the rigid body not change rotation
    30.         if (GetComponent<Rigidbody>())
    31.         {
    32.             GetComponent<Rigidbody>().freezeRotation = true;
    33.         }
    34.     }
    35.  
    36.     void ZoomOrthoCamera(Vector3 zoomTowards, float amount)
    37.     {
    38.         // Calculate how much we will have to move towards the zoomTowards position
    39.         float multiplier = (1.0f / this.GetComponent<Camera>().orthographicSize * amount);
    40.  
    41.         // Move camera
    42.         transform.position += (zoomTowards - transform.position) * multiplier;
    43.  
    44.         // Zoom camera
    45.         this.GetComponent<Camera>().orthographicSize -= amount;
    46.  
    47.         // Limit zoom
    48.         this.GetComponent<Camera>().orthographicSize = Mathf.Clamp(this.GetComponent<Camera>().orthographicSize, distanceMin,distanceMax);
    49.     }
    50.  
    51.  
    52.  
    53.  
    54.     void LateUpdate()
    55.     {
    56.  
    57.         if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
    58.  
    59.             ZoomOrthoCamera (Camera.main.ScreenToWorldPoint (Input.mousePosition), 1);
    60.  
    61.  
    62.         }
    63.  
    64.         if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
    65.  
    66.             ZoomOrthoCamera (Camera.main.ScreenToWorldPoint (Input.mousePosition),-1);
    67.  
    68.  
    69.         }
    70.  
    71.         if (target)
    72.         {
    73.             if (Input.GetMouseButton(0))
    74.             {
    75.                 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
    76.                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
    77.             }
    78.             rotationYAxis += velocityX;
    79.             rotationXAxis -= velocityY;
    80.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    81.             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    82.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    83.             Quaternion rotation = toRotation;
    84.  
    85.             //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
    86.             //RaycastHit hit;
    87.             //if (Physics.Linecast(target.position, transform.position, out hit))
    88.             //{
    89.             //    distance -= hit.distance;
    90.             //}
    91.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    92.             Vector3 position = rotation * negDistance + target.position;
    93.  
    94.             transform.rotation = rotation;
    95.             transform.position = position;
    96.             velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
    97.             velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
    98.         }
    99.     }
    100.     public static float ClampAngle(float angle, float min, float max)
    101.     {
    102.         if (angle < -360F)
    103.             angle += 360F;
    104.         if (angle > 360F)
    105.             angle -= 360F;
    106.         return Mathf.Clamp(angle, min, max);
    107.     }
    108. }
     
  3. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Or use this piece here..

    Code (CSharp):
    1. public static float ClampAngle(float angle, float min, float max)
    2.     {
    3.         if (angle < -360F)
    4.             angle += 360F;
    5.         if (angle > 360F)
    6.             angle -= 360F;
    7.         return Mathf.Clamp(angle, min, max);
    8.     }
    Angle beingyour xRotation
     
  4. Mergster

    Mergster

    Joined:
    Dec 8, 2016
    Posts:
    10
    Thanks for trying to help. I winded up using this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour {
    5.     public float speed = 6.0F;
    6.     public float baseSpeed;
    7.     public float rotateSpeed = 3.0F;
    8.     private Vector3 moveDirection = Vector3.zero;
    9.     private float rotatingStart;
    10.     private float rotatingUpdate;
    11.     float rotationX = 0;
    12.     float rotationY = 0;
    13.     float minRotationX = -45;
    14.     float maxRotationX = 45;
    15.         void Update()
    16.         {
    17.         rotationX += -Input.GetAxis("Vertical") * rotateSpeed * Time.deltaTime;
    18.         rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
    19.         rotationY += Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed; // added speed
    20.         transform.rotation = Quaternion.Euler(rotationX, rotationY, 0);
    21.         //make speed stay above 50
    22.         if (speed < 30) {
    23.             speed = 30;
    24.         }
    25.         //check if player is rotating along y and adjust speed
    26.         rotatingStart = transform.rotation.y;
    27.         if (rotatingStart != rotatingUpdate) {
    28.             speed -= 5;
    29.             rotatingUpdate = rotatingStart;
    30.         }
    31.         else {
    32.             if (speed < baseSpeed) {
    33.                 speed++;
    34.             }
    35.         }
    36.             //move player
    37.        
    38.             CharacterController controller = GetComponent<CharacterController>();
    39.        
    40.             transform.Translate(Vector3.forward * speed * Time.deltaTime);
    41.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical" ), 0);
    42.             moveDirection = transform.TransformDirection(moveDirection);
    43.             controller.Move(moveDirection * speed * Time.deltaTime);
    44.         }
    45.     }
     
  5. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    330
    Can a similar method work to limit rotation if its caused by gravity/physics instead of player input?