3rd person camera's y angle slowing character movement
I'm sure this is an easy fix, however I am currently stumped. I have a 3rd person camera set to orbit the player and a movement script that's supposed to move the player relative to the camera's orientation. The issue is that when the camera looks up/down, the player's move speed slows to a crawl. When I normalize playerMovement, the camera issue is fixed but the player then moves at a super high speed. When I don't normalize playerMovement, the speed is fixed but now the aforementioned camera problem occurs. Am I forgetting to add some calculation or something to that effect? Please advise me if you can. The movement script I have right now is this:
public float moveSpeed = 7f;
private Animator animator = null;
private CharacterController controller;
void Start()
{
animator = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
void Update()
{
MovePlayer();
}
void MovePlayer()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0, ver).normalized * moveSpeed * Time.deltaTime;
playerMovement = Camera.main.transform.TransformDirection(playerMovement);
playerMovement.y = 0;
controller.Move(playerMovement);
if (playerMovement != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(playerMovement), 0.2f);
animator.SetFloat("MovementSpeed", 1f * moveSpeed);
}
if (playerMovement == Vector3.zero)
{
animator.SetFloat("MovementSpeed", 0);
}
}
}
Your answer
Follow this Question
Related Questions
I can't seem to get this movement + automatic lookat the direction of movement work for a 2.5D game 0 Answers
I want to make an on click movement script with a grid type map need references to look at 0 Answers
Movement When Character is in air. 0 Answers
How do I get a character to walk on walls and ceilings? 1 Answer