using UnityEngine;

public class PlayerMove : MonoBehaviour {
	public Transform cam;
	public Transform body;
	public CharacterController pl;
	
	public float speed = 6F;
	public float sensitivityX = 6F;
	public float sensitivityY = 6F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	private float rotationY, rotationX;
	
	void Start ()
	{
		// Make the rigid body not change rotation
		if (rigidbody)
			rigidbody.freezeRotation = true;
		rotationX = body.localEulerAngles.y;
		rotationY = cam.localEulerAngles.x;
	}
	
	void Update ()
	{	
		if (Input.GetMouseButton(0))
			pl.Move(body.forward * Time.deltaTime * speed);
		if (Input.GetMouseButton(1))
			pl.Move(-body.forward * Time.deltaTime * speed);
		
		rotationX += Input.GetAxis("Mouse X") * sensitivityX;
		rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
		rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
		body.localEulerAngles = new Vector3(0, rotationX, 0);
		cam.localEulerAngles = new Vector3(-rotationY, 0, 0);
	}
}