Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Movement relative to camera

Discussion in 'Editor & General Support' started by Ellukitas_123, Mar 9, 2022.

  1. Ellukitas_123

    Ellukitas_123

    Joined:
    Jan 20, 2021
    Posts:
    21
    Hi,

    I'm starting a new game in 3D. I started with the controls but when I combine Cinemachine with my script it makes something that I don't like. When I press the key to move to right or left the player starts rotating, and i want to only move right. The movement is relative to the Cinemachine camera

    Video showing what happens: https://ellukitas-personal-public.s...ia+2/bugs/Relative+camera+position/Result.mp4

    Here's the code:
    Code (CSharp):
    1.  
    2.         if (movementDirection.magnitude >= 0.1f)
    3.         {
    4.             targetAngle = Mathf.Atan2(movementDirection.x, movementDirection.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    5.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothtime);
    6.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    7.  
    8.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    9.             characterController.Move(moveDir.normalized * playerSpeed * Time.deltaTime);
    10.         }
    And here's is a visual explanaition:


    If someone can help me I'll be very gratefull,
    Thank you.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,321
    Try changing the Binding Mode on your FreeLook to World Space.
     
  3. Ellukitas_123

    Ellukitas_123

    Joined:
    Jan 20, 2021
    Posts:
    21
    Oh, thank you for your quick response, and that solved the problem so thank you!