I need help with a player movement script that I’m using for the character’s rotation. It works alright, but I’ve ran into 2 problems with it:
- The player moves diagonally when I press the A & D keys.
- When wanting to move down by pressing S, the character turns in the direction, but he doesn’t move. I’ve only recently gotten into coding, so I’m not sure what the problem is. If someone could help, that would be amazing.
Here’s the code:
public class Player : MonoBehaviour
{
public float movementSpeed = 10f;
void Update()
{
ControllPlayer();
}
void ControllPlayer()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
if (moveHorizontal == 0 && moveVertical == 0) return;
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
if (movement != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);
}
transform.Translate(movement * movementSpeed * Time.deltaTime, Space.World);
}
}