Question by
ninedivinez · Dec 07, 2019 at 06:41 AM ·
movementmovement scriptplayer movement
Moving forward seems to be specific to the original direction of the object being controlled
So I have this code written up to move the player around and have them be able to look around, which works, but if I look to the left, pressing "W" still moves the player in the same direction. How can I adjust this so the player moves in whatever direction the player is facing? Any help is greatly appreciated!
Here is the code I have below:
public float movementRate = 2.0f;
public GameObject player;
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f;
private float rotX = 0.0f;
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
player.transform.position += new Vector3(horizontalInput * movementRate * Time.deltaTime, 0, 0);
player.transform.position += new Vector3(0, 0, verticalInput * movementRate * Time.deltaTime);
float mouseX = Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
Comment
Your answer
Follow this Question
Related Questions
Object rotates when moving sideways 0 Answers
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers
My player wont move and i dont know why 1 Answer
Reduce Player Movement in Air 0 Answers
Player only moves when the key is pressed, not when it's held. 0 Answers