0

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

Kevin
  • 5,689
  • 1
  • 8
  • 26
Jamelaumn
  • 15
  • 4

2 Answers2

1

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.

Kelly Thomas
  • 3,882
  • 1
  • 23
  • 35
Willem
  • 165
  • 4
  • 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 – Jamelaumn May 26 '23 at 14:11
  • 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: https://gamedev.stackexchange.com/questions/205760/how-to-have-a-great-and-simple-third-player-controller – Willem May 26 '23 at 19:53
  • thanks, it was helpful ;) – Jamelaumn May 26 '23 at 20:53
  • 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? – Jamelaumn May 26 '23 at 20:56
  • 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. – Willem May 26 '23 at 21:15
  • Programming is, each class is only responsible for what it is created for. That means, a player camera has nothing to do with a compass. And vice versa. But I hear you thinking, the compass needs to know the rotation of the player. For such I would create a reference at the compass to the player, where the player has a property - or just a public member to get the rotation (or y degrees).

    Programming is also; reading the official dull pages of microsoft, unity and others. That's where the magic is being explained.

    – Willem May 26 '23 at 21:15
0

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

Willem
  • 165
  • 4
Jamelaumn
  • 15
  • 4
  • 2
    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. – Kelly Thomas May 27 '23 at 03:40