Question by
cyth1 · Feb 05, 2016 at 04:08 PM ·
mouselookfps controller
Player walk direction does't follow mouse rotation.
I'm trying to get my player to follow my mouse rotation. When I press W and move forward, then rotate the camera to look to the right, my player continues walking in the previous direction(and now appears to be strafing left).
Here is my movement code:
public class FPSCam : MonoBehaviour {
public float speed = 2f;
private Vector3 movement;
public GameObject cameraObject;
void Update ()
{
//GetComponent<Rigidbody>().AddRelativeForce(Input.GetAxis("Horizontal") * speed, 0, Input.GetAxis("Vertical") * speed);
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
GetComponent<Rigidbody>().velocity = movement * speed;
/*movement = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
movement.z += speed;
}
if (Input.GetKey(KeyCode.S))
{
movement.z -= speed;
}
if (Input.GetKey(KeyCode.A))
{
movement.x -= speed;
}
if (Input.GetKey(KeyCode.D))
{
movement.x += speed;
}*/
//transform.Translate(movement * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent<MouseLook>().currentYRotation, 0);
}
}
and my mouselook code:
public class MouseLook : MonoBehaviour {
private float lookSensitivity = 5f;
private float yRotation;
private float xRotation;
public float currentYRotation;
private float currentXRotation;
private float yRotationV;
private float xRotationV;
private float lookSmoothDamp = 0.1f;
void Update ()
{
yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -90, 90);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothDamp);
currentYRotation = Mathf.SmoothDamp(currentYRotation, xRotation, ref yRotationV, lookSmoothDamp);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
}
Comment
Your answer