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

Question How do I find cinemachine forward on the x,z plane?

Discussion in 'Cinemachine' started by unkopath, Nov 13, 2022.

  1. unkopath

    unkopath

    Joined:
    Jun 23, 2018
    Posts:
    19
    Hi, I'm coding a third person controller that rotates in the direction of the camera (cinemachine free look component), I'm trying to move the player aswell in the direction of the camera but I can't get the planar direction from it (camera forward on the x,z plane) can someone helpme and tellme what i'm doing wrong please? Here is my code:

    Code (CSharp):
    1.  
    2.  
    3. Hi, I'm coding a third person controller that rotates in the direction of the camera (cinemachine free look component), I'm trying to move the player aswell in the direction of the camera but I can't get the planar direction from it (camera forward on the x,z plane) can someone helpme and tellme what i'm doing wrong please? Here is my code:
    4.  
    5.      using System.Collections;
    6.      using System.Collections.Generic;
    7.      using UnityEngine;
    8.    
    9.      public class PlayerController : MonoBehaviour
    10.      {
    11.          [Header("Movement")]
    12.          [SerializeField] private float walkSpeed = 2f;
    13.    
    14.          [Header("Sharpness")]
    15.          [SerializeField] private float rotationSharpness = 10f;
    16.          [SerializeField] private float moveSharpness = 10f;
    17.    
    18.          [SerializeField] private Animator animator;
    19.          [SerializeField] private PlayerInputs playerInputs;
    20.          [SerializeField] private CharacterController characterController;
    21.    
    22.          [SerializeField] private Camera playerCamera;
    23.    
    24.          private float targetSpeed;
    25.          private Quaternion targetRotation;
    26.    
    27.          private float newSpeed;
    28.          [SerializeField] private Vector3 newVelocity;
    29.          private Quaternion newRotation;
    30.    
    31.          private float strafeParameter;
    32.          private Vector3 strafeParametersXZ;
    33.    
    34.          private void Start()
    35.          {
    36.              animator.applyRootMotion = false;
    37.          }
    38.    
    39.          private void Update()
    40.          {
    41.              Vector3 moveInputVector = new Vector3(playerInputs.MoveAxisRightRaw, 0f, playerInputs.MoveAxisForwardRaw);
    42.    
    43.              Vector3 cameraPlanarDirection = Quaternion.Euler(0f, playerCamera.transform.rotation.eulerAngles.y, 0f) * transform.forward;
    44.              Quaternion cameraPlanarRotation = Quaternion.LookRotation(cameraPlanarDirection);
    45.    
    46.              Vector3 moveInputVectorOriented = cameraPlanarRotation * moveInputVector.normalized;
    47.    
    48.              //Move Speed
    49.              targetSpeed = moveInputVector != Vector3.zero ? walkSpeed : 0;
    50.              newSpeed = Mathf.Lerp(newSpeed, targetSpeed, Time.deltaTime * moveSharpness);
    51.    
    52.              //Velocity
    53.              newVelocity = moveInputVectorOriented * newSpeed;
    54.              characterController.Move(newVelocity * Time.deltaTime);
    55.    
    56.              //Rotation
    57.              transform.rotation = Quaternion.Euler(0f, playerCamera.transform.rotation.eulerAngles.y, 0f);
    58.    
    59.              //Animations
    60.              strafeParameter = Mathf.Clamp01(strafeParameter + Time.deltaTime * 4);
    61.              strafeParametersXZ = Vector3.Lerp(strafeParametersXZ, moveInputVector * newSpeed, moveSharpness * Time.deltaTime);
    62.    
    63.              animator.SetFloat("Strafing", strafeParameter);
    64.              animator.SetFloat("StrafingX", Mathf.Round(strafeParametersXZ.x * 100f) / 100f);
    65.              animator.SetFloat("StrafingZ", Mathf.Round(strafeParametersXZ.z * 100f) / 100f);
    66.          }
    67.      }
    68.    
    69.  
    70.  
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Use the main camera transform, not the FreeLook's transform. Try something like this:
    Code (CSharp):
    1. var fwd = Camera.main.transform.forward;
    2. fwd.y = 0;
    3. fwd.normalize(); // if you want a unit-length vector
     
    Divij-manik likes this.