Question by
bunnybean · Feb 22, 2019 at 01:44 AM ·
cameramovementmovement scriptwasd
WASD and camera movement
I want my player to be able to move with WASD and look around with the mouse. My player can do that but the problem is the player's movement doesn't change to go with where they are looking, it stays along the axis. I'll post my code below, but I'm struggling with how to get those lines of code to interact. I want my keys to be the same, but the player moves forward if they are looking forward, ya know?
This is my player movement script
public class playermovin : MonoBehaviour
{
public float speed = 4f;
private void Update()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
}
}
And this is the camera mouse movement script
public class lookinaround : MonoBehaviour
{
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f; // rotation around the up/y axis
private float rotX = 0.0f; // rotation around the right/x axis
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
}
void Update()
{
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;
}
}
Thank you so much for the help!
Comment