Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why is my camera drifting?

Discussion in 'Getting Started' started by Altissimus, May 6, 2019.

  1. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    I'm using a camera to drive player movement as per the code below, but rather than travelling in a straight line forward the player actually travels in towards 2 o'clock (assuming 12 noon is forward) - and I have no idea why.

    As is probably obvious, I'm new to this stuff...please could you kindly explain any answers using small words. Thanks :)

    Code (CSharp):
    1. public class CameraController : MonoBehaviour
    2. {
    3.     public Transform target;
    4.     public Vector3 offset;
    5.     public bool useOffsetValues;
    6.     public float rotateSpeed;
    7.  
    8.     public Transform pivot;
    9.  
    10.     //public float speed;
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         if (!useOffsetValues){
    16.         offset = target.position - transform.position;
    17.         }
    18.  
    19.         pivot.transform.position = target.transform.position;
    20.         pivot.transform.parent = target.transform;
    21.  
    22.         //hide mouse pointer
    23.         Cursor.lockState = CursorLockMode.Locked;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void LateUpdate()
    28.     {
    29.         // get the x position of the mouse and rotate the target
    30.         float horizontal = Input.GetAxisRaw("Mouse X")*rotateSpeed;
    31.         target.Rotate(0,horizontal,0);
    32.  
    33.         //Get the Y position of the mouse & rotate pivot for camera
    34.         float vertical = Input.GetAxisRaw("Mouse Y")*rotateSpeed;
    35.         pivot.Rotate(-vertical,0,0);
    36.  
    37.         // move the camera based on the current rotation of the target and the original offset
    38.         float desiredYAngle = target.eulerAngles.y;
    39.         float desiredXAngle = pivot.eulerAngles.x;
    40.         Quaternion rotation = Quaternion.Euler(desiredXAngle,desiredYAngle,0);
    41.         transform.position = target.position-rotation*offset;
    42.  
    43.         if(transform.position.y < target.position.y){
    44.             transform.position = new Vector3(transform.position.x, target.position.y-.5f, transform.position.z);
    45.         }
    46.        
    47.         transform.LookAt(target);
    48.  
    49.  
    50.         //transform.position = target.position - offset;
    51.  
    52.  
    53.     }
    54. }
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.  
    4.     public float moveSpeed;
    5.     public float jumpForce = 5f;
    6.     public CharacterController controller;
    7.  
    8.     Vector3 moveDirection;
    9.     public float gravityScale;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.         controller = GetComponent<CharacterController>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         float yStore = moveDirection.y;
    23.         moveDirection = (transform.forward*Input.GetAxisRaw("Vertical")+transform.right*Input.GetAxisRaw("Horizontal"));
    24.         moveDirection = moveDirection.normalized*moveSpeed;
    25.         moveDirection.y = yStore;
    26.  
    27.           if(controller.isGrounded)
    28.         {
    29.             moveDirection.y = 0f;
    30.           if(Input.GetButtonDown("Jump"))
    31.           {
    32.             moveDirection.y = jumpForce;
    33.      
    34.           }
    35.         }
    36.         moveDirection.y = moveDirection.y + Physics.gravity.y*gravityScale*Time.deltaTime;
    37.         controller.Move(moveDirection*Time.deltaTime);
    38.     }
    39. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This line doesn't make sense to me and I would assume is the cause of your problem:

    Code (csharp):
    1. moveDirection = (transform.forward*Input.GetAxisRaw("Vertical")+transform.right*Input.GetAxisRaw("Horizontal"));
     
  3. Altissimus

    Altissimus

    Joined:
    May 11, 2015
    Posts:
    49
    Hi Joe, could you elaborate please? This line does work.