I know this has been asked a million times, however I can’t find the right solution that fits my code.
I want the player to be able to rotate with the right mouse button which does great! The only problem I am having is when I rotate the player the movement does not go in that direction. It’s still locked in the original position.
using UnityEngine;
using System.Collections;
public class ThirdPerson : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
CharacterController controller;
public float speed = 20.0f;
Vector3 movement = Vector3.zero;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire2"))
{
Quaternion rot = new Quaternion();
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
movement.x = Input.GetAxis("Horizontal") * speed;
movement.z = Input.GetAxis("Vertical") * speed;
movement.y += Physics.gravity.y;
controller.Move(movement * Time.deltaTime);
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
if (hit.moveDirection.y < -0.3f)
return;
}
}