Search Unity

Can't Get Camera to NOT Move Past Shoulders

Discussion in 'Scripting' started by KyleLeitao, Aug 22, 2018.

  1. KyleLeitao

    KyleLeitao

    Joined:
    Aug 10, 2018
    Posts:
    4
    0favorite


    I am trying to get my camera to not rotate past both of the player's shoulders. When I try to make the camera not move past the right shoulders it works perfectly but when I try to make it not move past the left shoulders it doesn't go past 0* and it will reset back to 89*. I have no clue why it won't work I even tried making it all positive and doesn't get anywhere close to this. Can someone please help?

    Code (CSharp):
    1.    public Transform target;
    2.    Vector3 cameraAngles = transform.rotation.eulerAngles;
    3.    cameraAngles.z = 0;
    4.    Vector3 targetAngles = target.transform.rotation.eulerAngles;
    5.    targetAngles.z = 0;
    6.  
    7.    //Will not go past 0* it will revert back to 89*
    8.    if (cameraAngles.y >= (targetAngles.y + 89))
    9.    {
    10.        cameraAngles.y = targetAngles.y + 89;
    11.    }
    12.  
    13.    if (cameraAngles.y <= (targetAngles.y - 89))
    14.    {
    15.        cameraAngles.y = targetAngles.y - 89;
    16.    }
    17.  
    18.    transform.eulerAngles = cameraAngles;
    19.    transform.RotateAround(target.transform.position, Vector3.up, rotationY);
    20.    transform.RotateAround(target.transform.position, Vector3.left, rotationX);
    21.    transform.LookAt(target.transform.position);
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Here's a previous post I've made on this subject: https://forum.unity.com/threads/pre...above-specific-angles-3d.541580/#post-3571384

     
  3. KyleLeitao

    KyleLeitao

    Joined:
    Aug 10, 2018
    Posts:
    4
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Literally the exact same principle applies. If you want to clamp the yaw, then clamp the yaw.