Search Unity

how to clamp my rotation x

Discussion in 'Scripting' started by keizerxjwz, Jan 17, 2021.

  1. keizerxjwz

    keizerxjwz

    Joined:
    Jan 17, 2021
    Posts:
    5
    hello, so i wanted to clamp my x rotation between 9,-10 so i dont have a player that faceplants his self into the ground (see the photo down bellow).quarternions and transform is al a bit new for me so i dont know how to do this i tryed to google it but that didnt work out

    but its really about the peace at the tranform.rotation i want there the clamping
    Code (CSharp):
    1.  moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    2.         if (im.rightClick)
    3.         {
    4.             allowPlayerRotation = 0.008f;
    5.         }
    6.         if (!im.rightClick)
    7.         {
    8.             allowPlayerRotation = 0.1f;
    9.         }      
    10.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(cam.transform.forward),allowPlayerRotation);
    11.         moveDir = transform.TransformDirection(moveDir);
    upload_2021-1-17_14-27-22.png

    anny one ideas?
     

    Attached Files:

  2. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    Don't bother looking at the rotation in Inspector, it'll only cause more confusion when dealing with rotation from my experience.

    Use Debug.Log(transform.rotation) if you want to understand why your code doesn't work. If I recall transform.rotation returns 0 to 1 not in degrees. From Unity Doc - "If you want to match values you see in the Inspector, use the Quaternion.eulerAngles".