0
\$\begingroup\$

I have a player with third and first person camera, I want to make my player move in the direction that my camera is looking at when I hold the right mouse in third person and move(Like my player is always looking forward based on where my camera is). But my player does not rotate, and I wish to implement it, I want help so that my player rotate In the current direction my camera is looking at. code:

code:

    using UnityEngine;
    using UnityEngine.InputSystem;
    public class PlayerCamera : MonoBehaviour
    {
        public GameObject[] cameras;
        public bool PlayerShouldRotateByCameraAngle = false;
        [Tooltip("As It's the main camera that rotates (cinemachinebrain), it should be the main camera")]
        public Transform cameraTransform;
        private int currentIndex;
    
        public float lookSpeed = 2f;
        public float lookXLimit = 90;
        private float rotationX = 0;
    
        private void Awake() {
            CursorLocker(true);
        }
    
        private void Start() {
            currentIndex = 1;
        }
        public void OnChangeCamera(InputAction.CallbackContext value) {
            if (value.started) {
               cameras[currentIndex].SetActive(false);
               currentIndex = (currentIndex + 1) % cameras.Length;
               cameras[currentIndex].SetActive(true);
            }
        }
        private void CursorLocker(bool shouldLock) {
            Cursor.lockState = shouldLock ? CursorLockMode.Locked : CursorLockMode.None;
        }
    
        public Vector3 CameraRotation(Vector3 direction) {
            if (PlayerShouldRotateByCameraAngle) {
                rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
                rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
                cameras[currentIndex].transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
                return Quaternion.AngleAxis(cameras[currentIndex].transform.rotation.eulerAngles.y, Vector3.up) * direction;
            }
    
            return direction;
        }
    }

public class PlayerMovement : MonoBehaviour
{
    [Header("Config Player")]

    [SerializeField] private float speed = 3f;
    [Tooltip("This is the Time for the player rotation - it makes smooth")][SerializeField] private float smoothTime = 0.05f;
    [SerializeField] private GameObject playerCameraObject;

    private PlayerCamera playerCamera;
    private CharacterController controller;
    private float currentSpeedRotationing;
    private float verticalVelocity = 0f;
    private Vector2 movement2D;
    private Vector3 direction;

    private bool IsGrounded() => controller.isGrounded;
    private void Awake() {
        controller = GetComponent<CharacterController>();
    }

    private void Start() {
        playerCamera = playerCameraObject.GetComponent<PlayerCamera>();
    }

    void FixedUpdate() {
        ApplyMovement();
        ApplyRotation();
    }

    public void OnMovement(InputAction.CallbackContext value)
    {
        movement2D = value.ReadValue<Vector2>();
        movement2D.Normalize();
        direction = new Vector3(movement2D.x, 0, movement2D.y);
        direction.Normalize();
    }

    public void OnShouldWalkBasedOnCamera(InputAction.CallbackContext value) {
        if (value.performed)
        {
            playerCamera.PlayerShouldRotateByCameraAngle = true;
        }
        if (value.canceled)
        {
            playerCamera.PlayerShouldRotateByCameraAngle = false;
        }
    }

    void ApplyMovement() {
        Vector3 currentDirection = playerCamera.CameraRotation(direction);
    }

    void ApplyRotation() {
        if (movement2D.sqrMagnitude == 0) return;

        float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
        float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentSpeedRotationing, smoothTime);
        transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
    }
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$
private Vector3 getDirection(Transform t, bool keepYhorizontal = false)
{

    Vector3 direction = t.rotation * t.forward;
    
    if(keepYhorizontal )
        direction.y = 0;

    return direction;

}

Multiple the quaternion my a directional vector. This multiplycation isn't actually multiplying. The order is important, otherwise the compiler will give an error.

Made the function so it can be used for any transform and so also for a camera.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .