Search Unity

My character controller moves in circles instead of moving based of the direction of the camera

Discussion in 'Scripting' started by KrisTheCoolGuy, Feb 4, 2022.

  1. KrisTheCoolGuy

    KrisTheCoolGuy

    Joined:
    Feb 16, 2020
    Posts:
    10
    currently i'm trying to move my character controller based of the direction of the camera, i sort off got it but instead of moving, it just moves on circles, i tried looking up for help but i couldn't find why:
    Code (CSharp):
    1.         /* Check if we are sprinting or not and assign it to a variable */
    2.         float movementState = ( IsInternalCrouching ? crouchSpeed : IsSprinting ? sprintSpeed : walkSpeed );
    3.  
    4.         /*  Set our currentInput for Vertical/Horizontal */
    5.         currentInput = new Vector2( movementState * Input.GetAxis( "Vertical" ), movementState * Input.GetAxis( "Horizontal" ) );
    6.  
    7.         float moveDirectionY = moveDirection.y;
    8.         moveDirection = ( transform.TransformDirection( Vector3.forward ) * currentInput.x ) + ( transform.TransformDirection( Vector3.right ) * currentInput.y );
    9.         moveDirection.y = moveDirectionY;
    10.  
    11.         targetAngle = Mathf.Atan2( moveDirection.x, moveDirection.z ) * Mathf.Rad2Deg + playerCameraTransform.eulerAngles.y;
    12.         float smoothAngle = Mathf.SmoothDampAngle( transform.eulerAngles.y, targetAngle, ref playerTurnSmoothVelocity, playerTurnSmoothTime );
    13.         transform.rotation = Quaternion.Euler( 0f, smoothAngle, 0f );
    14.  
    15.           /* Move our character based of our angle view */
    16.          Vector3 moveDirectionTP = Quaternion.Euler( 0f, targetAngle, 0f ) * Vector3.forward;
    17.  
    18.         /* Setup our Character gravity and movement */
    19.         if ( !characterController.isGrounded )
    20.              moveDirectionTP.y -= gravity * Time.deltaTime;
    21.  
    22.        /* Handles if we are on slopes or no */
    23.        if ( canSlideOnSlopes && IsSliding )
    24.              moveDirectionTP += new Vector3( hitPointNormal.x, -hitPointNormal.y, hitPointNormal.z ) * slopeSpeed;
    25.  
    26.        characterController.Move( moveDirectionTP * Time.deltaTime );
    i attach my full player controller script for full context
     

    Attached Files:

  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Hard to tell what the issues is just reading the code - might be you need the the rotation delta from player to camera on line 11? Right now you are using absolute camera rotation and not taking current player rotation into account. I think.

    My brain isn't that great reasoning about transformations, rotations and spatial/project stuffin general without visual aids so for issues like this I almost always turn to using Debug.DrawLine to visualise what's going on. No need to hold it all in your head. Draw out the direction vectors in different colors so you can see if you're transforming them correctly.

    You can also use transform.forward / transform.right / transform.up etc to get the world space forward / right / up vectors of any transform.

    Another alternative to rotating direction vectors is to get the xz plane normal from camera to player and go from there:

    Code (CSharp):
    1. var forward = playerCameraTransform.position  - transform.position;
    2. forward.y = 0;
    3. forward = forward.normalized;
    4. var right = Vector3.cross(forward, Vector3.up).normalized;
    But that would make constraining rotation velocity a little more difficult.