Reputation: 3563
I have 2 scripts, which make player move like in the FPS game. But it don't move to that direction, to which player are looking - it always move to the same direction, regardless of the direction of the camera..
mouselook.cs
float yRotation;
float xRotation;
float lookSensitivity = 5;
float currentXRotation;
float currentYRotation;
float yRotationV;
float xRotationV;
float lookSmoothnes = 0.1f;
void Update ()
{
yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
xRotation = Mathf.Clamp(xRotation, -80, 100);
currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, ref xRotationV, lookSmoothnes);
currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, ref yRotationV, lookSmoothnes);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
playermovement.cs
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Upvotes: 2
Views: 14636
Reputation: 394
make your mouse look like this
float lookSensitivity = 5;
float mouseX,mouseY;
public Transform playerBody;
float xRotation = 0f;
void Update ()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSentivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSentivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
save this and Drag your Player from hierarchy to the camera script playerBody.
Upvotes: 1
Reputation: 1
Doesn't it need like a requirecomponentTypeof or something like that
Upvotes: 0