using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

[RequireComponent(typeof(CharacterController))]

public class PlayerScript : MonoBehaviour {

private Vector3 RotateClamp;

public float Speed;

public float RotateSpeed;

public Text HelpText;

private float MoveSpeed;

public float UsedXClampMin;

public float UsedXClampMax;

public float UsedZClampMin;

public float UsedZClampMax;

public float XClampMin;

public float XClampMax;

public float ZClampMin;

public float ZClampMax;

public float ServeXClampMin;

public float ServeXClampMax;

public float ServeZClampMin;

public float ServeZClampMax;

public float RotateClampMin;

public float RotateClampMax;

public Transform ServePosition;

public Rigidbody Ball;

public float ServeForce;

public Collider ServeCatch;

public bool Served;

public bool BallInAir;

public bool BallInPlay;

// Use this for initialization

void Start ()

{

}

IEnumerator Serve()

{

Rigidbody ServedBall = (Rigidbody) Instantiate (Ball, ServePosition.position, ServePosition.rotation);

ServedBall.velocity = transform.up * ServeForce;

yield return new WaitForSeconds(.1f);

ServeCatch.enabled = true;

yield return null;

}

// Update is called once per frame

void Update ()

{

CharacterController Controller = GetComponent<CharacterController> ();

//basic movement

Vector3 Sideways = Vector3.left;

float MoveHorizontal = MoveSpeed * Input.GetAxis ("Horizontal");

Controller.SimpleMove (Sideways * MoveHorizontal);

Vector3 Forward = Vector3.back;

float MoveVertical = MoveSpeed * Input.GetAxis ("Vertical");

Controller.SimpleMove (Forward * MoveVertical);

//clamp your position so you cant fall off

transform.position = new Vector3 ((Mathf.Clamp(transform.position.x, UsedXClampMin, UsedXClampMax)), transform.position.y, Mathf.Clamp(transform.position.z, UsedZClampMin, UsedZClampMax));

//rotate the player with the mouse

if (Input.GetAxis ("Mouse X") < 0)

{

transform.Rotate (Vector3.up * RotateSpeed);

}

if (Input.GetAxis ("Mouse X") > 0)

{

transform.Rotate (Vector3.up * -RotateSpeed);

}

//lock the cursor

Cursor.lockState = CursorLockMode.Locked;

//clamp your rotation

Vector3 rot = transform.rotation.eulerAngles;

float yrotation = Mathf.Clamp (rot.y, RotateClampMin, RotateClampMax);

if (yrotation < 0)

{

yrotation = 360 + yrotation;

}

rot.y = yrotation;

transform.rotation = new Quaternion(transform.rotation.x, rot.y, transform.rotation.z, transform.rotation.w);

//before you served

if (!Served) {

//move slower before serving.

MoveSpeed = Speed / 2;

//clamp your position before serving

if (Input.GetButtonDown ("Serve")) {

StartCoroutine ("Serve");

Served = true;

BallInAir = true;

}

//give you a smaller area to move in if you havent served:

UsedXClampMin = ServeXClampMin;

UsedXClampMax = ServeXClampMax;

UsedZClampMin = ServeZClampMin;

UsedZClampMax = ServeZClampMax;

} else

{

MoveSpeed = Speed;

UsedXClampMin = XClampMin;

UsedXClampMax = XClampMax;

UsedZClampMin = ZClampMin;

UsedZClampMax = ZClampMax;

}

//if the ball is in the air, dont move.

if (BallInAir)

{

MoveSpeed = 0;

HelpText.text = "Left mouse to hit";

}

}