﻿using UnityEngine;
using System.Collections;
 
public class CharacterMotor : MonoBehaviour {
	
	public float speed = 3.0f;
	public float runSpeed = 6.0f;
	public float jumpSpeed = 8.0F;
	public float rotSpeed = 90.0f; // turn at 90 degrees/second
	public float gravity = 20.0F;

	public Animator animator;
	
	private Vector3 moveDirection = Vector3.zero;
	private CharacterController controller;


	void Start() {
		controller = GetComponent<CharacterController>();
	}

	void Update() {
		// rotate character with Horizontal keys:
		transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
		CharacterController controller = GetComponent<CharacterController>();
		if (controller.isGrounded) {
			moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			if ( Input.GetKey(KeyCode.LeftShift) ) moveDirection *= runSpeed;
			else moveDirection *= speed;
			if ( Input.GetAxis("Vertical") != 0 ) {
				if ( !Input.GetKey(KeyCode.LeftShift) ) {
					animator.SetBool("Walk", true);
					animator.SetBool("Run", false);
				} else {
					animator.SetBool("Walk", false);
					animator.SetBool("Run", true);
				}
			} else {
			 	animator.SetBool("Walk", false);
				animator.SetBool("Run", false);
			}
			animator.SetBool("Jump", false);
			if (Input.GetButton("Jump")){
			    moveDirection.y = jumpSpeed;
			    
			}
		} else {
			animator.SetBool("Jump", true);
		}
		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);
	}
}