FANDOM



/** @since February 8th 2014
 * @author Ian Wong, Mamoon Alheti, Nikola Neskovic
 * 
 * 
 * 
 **/
using UnityEngine;
using System.Collections;
using System;

public abstract class PlayerBehaviour : MonoBehaviour
{
	private float moveSpeed, turnSpeed;
	
	Quaternion targetRotation;

	Vector3 newPosition;

	public PlayerBehaviour ()
	{
	}

	//TODO consider removing thisTransform... I don't think you need a reference. 
	protected IEnumerator basicMovePlayer(object[] parameters){
		//Transform thisTransform, Vector3 startPos, Vector3 endPos, float time
		//Transform thisTransform = (Transform)parameters [0];
		Vector3 startPos = transform.position;
		Vector3 endPos = (Vector3)parameters [0];
		float time = (float)parameters [1];

		
		//
		if((transform.position - endPos).magnitude > (transform.localScale.z / 2))
		{

			float i = 0.0f;
			float rate = 1.0f / time;
			targetRotation = Quaternion.LookRotation(endPos - transform.position);

			while(i < 1.0) {
				i += Time.deltaTime * rate;

				transform.position = Vector3.Lerp(startPos, endPos, i);

				transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed*i);
			
				yield return null;
			}
		}
	}

	// Use this for initialization
	void Start () {
		moveSpeed = 275f;
		turnSpeed = 2.5f;
		newPosition = new Vector3(-5, 3, 0);
	}
	
	// Update is called once per frame
	void Update () {
		/* If the left mouse button is pressed then access the mouse position variable from
		 * the camera behaviour script and set the local value of the mouse position to that value.
		 * Determine the distance from the starting point and the ending point and divide by the
		 * speed in order to get the time required to LERP to that point. Create and initialize 
		 * object array that will be passed to the coroutine then stop the coroutine in case it
		 * was previously called. Call the coroutine to move the character.
		 */
		if (Input.GetMouseButtonDown (1) || Input.GetMouseButton (1)){
			print("Click!");
			newPosition = CameraBeh.mousepos;
			newPosition += new Vector3(0, transform.localScale.y, 0);
			float distance = Vector3.Distance(transform.position, newPosition);
			float time = distance/moveSpeed;
			object[] parameters = {newPosition, time};
			base.StopCoroutine("basicMovePlayer");
			base.StartCoroutine("basicMovePlayer", parameters);
		}
	}
}

  This was the original Lerp and Slerp routine for moving and rotating a character at a constant rate.