﻿using UnityEngine;
using System.Collections;

public class PFCharacterController : MonoBehaviour {
  /**
   * Public variables
   */
  public float speed = 10f;
  public float speedOfRotation = 2f;
  public Transform head;

  /**
   * Private variables
   */
  private CharacterController controller = null;
  // Use this for initialization
  void Start () {
    controller = gameObject.GetComponent<CharacterController> ();
    if (!controller) {
      Debug.LogError ("Missing Character Controller component");
    }

    if (!head) {
      Debug.LogError ("Missing reference to 'head'");
    }
  }
	
  // Update is called once per frame
  void Update () {
    // Get the device input such as keyboard
    Vector3 input = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
    // Get the mouse input
    Vector2 mouseInput = new Vector2 (Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"));

    // Rotate the main object towards the x axis
    transform.Rotate (Vector3.up, mouseInput.x * speedOfRotation);
    // Rotate the head object towards the y axis
    head.Rotate (Vector3.left, mouseInput.y * speedOfRotation);
    // Move the chacter in the direction of the head and 
    gameObject.GetComponent<CharacterController> ().Move (transform.TransformDirection (input * speed * Time.deltaTime));
  }
}
