Search Unity

Camera keeps moving to behind player when looking up and down all the way, please help

Discussion in 'Scripting' started by Tempiz101, Aug 29, 2019.

  1. Tempiz101

    Tempiz101

    Joined:
    Aug 29, 2019
    Posts:
    2
    Ok so im very new to this and i have no clue how to fix this lol. I was following a tutorial and everything seems the same how the guy had it but for some reason, when im looking all the way down or up, the camera jumps directly behind the player, either down or up. Ima leave the code here in case someone doesnt mind fixing whatever i did wrong lol, thx!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public Vector3 offset;
    9.     public Transform pivot;
    10.  
    11.     public bool useOffsetValues;
    12.  
    13.     public float rotateSpeed;
    14.  
    15.     public float maxViewAngle;
    16.     public float minViewAngle;
    17.  
    18.     public bool invertY;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         if (!useOffsetValues)
    24.         {
    25.             offset = target.position - transform.position;
    26.         }
    27.  
    28.         pivot.transform.position = target.transform.position;
    29.         //pivot.transform.parent = target.transform;
    30.         pivot.transform.parent = null;
    31.  
    32.         Cursor.lockState = CursorLockMode.Locked;
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void LateUpdate()
    37.     {
    38.         pivot.transform.position = target.transform.position;
    39.  
    40.         //Get the X position of the mouse & rotate the target
    41.         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    42.         pivot.Rotate(0, horizontal, 0);
    43.        
    44.         //Get the Y position of the mouse & rotate the pivot
    45.         float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    46.         pivot.Rotate(-vertical, 0, 0);
    47.  
    48.         if (invertY)
    49.         {
    50.             pivot.Rotate(vertical, 0, 0);
    51.         }
    52.         else
    53.         {
    54.             pivot.Rotate(-vertical, 0, 0);
    55.         }
    56.  
    57.         //Limit up/down camera rotation
    58.         if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
    59.         {
    60.             pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
    61.         }
    62.  
    63.         if (pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
    64.         {
    65.             pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
    66.         }
    67.  
    68.         //Move the camera on the current rotation of the target & the orginal offset
    69.         float desiredYAngle = pivot.eulerAngles.y;
    70.         float desiredXAngle = pivot.eulerAngles.x;
    71.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    72.         transform.position = target.position - (rotation * offset);
    73.  
    74.         if (transform.position.y < target.position.y)
    75.         {
    76.             transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
    77.         }
    78.  
    79.         transform.LookAt(target);
    80.     }
    81. }
    82.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What inspector values are you using for maxViewAngle and minViewAngle? You need to set those limits to values which will prevent the issue you are describing.
     
  3. Tempiz101

    Tempiz101

    Joined:
    Aug 29, 2019
    Posts:
    2
    60 and -30, I'm not sure exactly I have to go back and look again later