0
\$\begingroup\$

I'm facing the problem that my chacter is spinning around itself when I try to move, that's because Im trying to make my player move in the direction of the camera, but when I move it keeps rotating around itself infinetly

Resuming my code:

public class PlayerMovement : MonoBehaviour
{
    [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 Vector2 movement2D;
    private Vector3 direction;

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

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

    void ApplyMovement()
    {
        Vector3 currentDirection = playerCamera.DirectionToMoveByCamera(direction);
        controller.Move(currentDirection * currentSpeed * Time.deltaTime);
    }

    void ApplyRotation()
    {

        if (movement2D.sqrMagnitude == 0) return;

        if (playerCamera.PlayerShouldRotateByCameraAngle && !playerCamera.isFirstPerson)
        {
            Debug.Log("sssssss");
            transform.rotation = Quaternion.Euler(0.0f, playerCamera.cameraTransform.transform.eulerAngles.y, 0.0f);
        }
        else if (playerCamera.isFirstPerson)
        {
            Debug.Log("should rotate");
        }
        else
        {
            float targetAngle = Mathf.Atan2(directiion.x, direction.z) * Mathf.Rad2Deg;
            float angle = Mathf.LerpAngle(transform.eulerAngles.y, targetAngle, smoothTime * Time.deltaTime);
            transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);
        }
    }

    void FixedUpdate()
    {
        ApplyMovement();
        ApplyRotation();
    }
}
    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;
        public bool isFirstPerson = false;

        private void Awake()
        {
            CursorLocker(true);
        }
    public Vector3 DirectionToMoveByCamera(Vector3 direction)
    {
        if (PlayerShouldRotateByCameraAngle || isFirstPerson)
        {
            return Quaternion.AngleAxis(cameraTransform.transform.rotation.eulerAngles.y, Vector3.up) * direction;
        }

        return direction;
    }

Image for context

enter image description here

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

By any chance, did you parent the Camera to the PlayerMovement.gameObject.transform?

Also the problem won't be in the if-statement, as you are overriding it here:

float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
float angle = Mathf.LerpAngle(transform.eulerAngles.y, targetAngle, smoothTime * Time.deltaTime);
transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f);

Unless you have playerCamera.PlayerShouldRotateByCameraAngle set to true.

Your scripts looks overly complicated. Try to reduce them. See no reason why this is in two scripts either.

When public Transform cameraTransform; should always be the main camera, consider:

public void Awake()
{
     cameraTransform = Camera.main.transform;
}

Also, always keep your indentations right, including the brackets, it will help you so much in keeping overview of your code.

\$\endgroup\$
6
  • \$\begingroup\$ I edited the code a little bit please take a look. The main camera is being passed in public Transform cameraTransform, the playerCamera.PlayerShouldRotateByCameraAngle is because I can like keep my player going in the same direction even if I change camera in third person, and if trtue, my player will move in the direction of where my camera is pointing at \$\endgroup\$
    – Jamelaumn
    May 26, 2023 at 14:11
  • \$\begingroup\$ Would update both the PlayerMovement.transform its position and rotation, rather then splitting them. The camera, as long as it is attached to the gameObject wil nicely follow along. Take a look at this question: gamedev.stackexchange.com/questions/205760/… \$\endgroup\$
    – Willem
    May 26, 2023 at 19:53
  • \$\begingroup\$ thanks, it was helpful ;) \$\endgroup\$
    – Jamelaumn
    May 26, 2023 at 20:53
  • \$\begingroup\$ since I cant comment on your answer there I will make it here, but what if like I want to bind all the postive and negative values to the current rotation? that code will do that? like the new "movement compass" will be redirected to the rotation? \$\endgroup\$
    – Jamelaumn
    May 26, 2023 at 20:56
  • \$\begingroup\$ Websites like this you should learn. You can't comment here because you can't at the other question. What you are asking is worth an entire new question, even if it's not code related. Even without code add the tags Unity and C# in any case. \$\endgroup\$
    – Willem
    May 26, 2023 at 21:15
0
\$\begingroup\$

Since my camera was inside my player it was also rotating itself when I was rotating my player.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Hi Jamelaumn, This would be better placed as a comment to Willem's answer that suggested the camera may be a child of the player. \$\endgroup\$ May 27, 2023 at 3:40

You must log in to answer this question.

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