Last active
August 29, 2015 14:10
-
-
Save LucienLee/dff8f0e4d86d1574df54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ThirdPersonController : MonoBehaviour { | |
public GameObject player; | |
public GameObject mainCamera; | |
public GameObject cameraCollisionBox; | |
public CollisionCounter cameraProbe; | |
public CollisionCounter probe; | |
public float Maxdistance; | |
public float moveSpeed; | |
public float verticalRatio; | |
void CalculateHeight(){ | |
Vector3 originVector = mainCamera.transform.position; | |
Vector3 localVector = mainCamera.transform.localPosition; | |
// REMOVE Y to caculate XZ magnitude | |
originVector.y = player.transform.position.y; | |
float y = Maxdistance - (originVector - player.transform.position).magnitude; | |
localVector.y=y; | |
mainCamera.transform.localPosition = localVector*verticalRatio; | |
} | |
// Update is called once per frame | |
void Update () { | |
// GET INPUT AXIS | |
float vertical = Input.GetAxis ("Vertical"); | |
float horizontal = Input.GetAxis ("Horizontal"); | |
// GET DIRECTION | |
Vector3 forward = mainCamera.transform.forward; | |
Vector3 right = mainCamera.transform.right; | |
//Calibrate Y AXIS | |
forward.y = 0; | |
right.y = 0; | |
// GET DIRECTION UNIT VECTOR | |
forward.Normalize (); | |
right.Normalize (); | |
Vector3 distance = (mainCamera.transform.position - player.transform.position); | |
distance.y = 0; | |
if (vertical > 0) { | |
player.rigidbody.velocity = forward * moveSpeed; | |
//Camera chases the player | |
if (distance.magnitude > Maxdistance) { | |
float originY = cameraCollisionBox.transform.position.y; | |
Vector3 newPosition = player.transform.position + distance.normalized * Maxdistance; | |
newPosition.y = originY; | |
cameraCollisionBox.transform.position = newPosition; | |
} | |
CalculateHeight(); | |
} else if (vertical < 0) { | |
player.rigidbody.velocity = -forward * moveSpeed; | |
if( cameraProbe.counter == 0 ){ | |
//Camera chases the player | |
float originY = cameraCollisionBox.transform.position.y; | |
Vector3 newPosition = player.transform.position + distance.normalized * Maxdistance; | |
newPosition.y = originY; | |
cameraCollisionBox.transform.position = newPosition; | |
}else{ | |
CalculateHeight(); | |
} | |
} | |
if (horizontal > 0){ | |
if( probe.counter > 0 ){ | |
cameraCollisionBox.transform.position += -right*moveSpeed*Time.deltaTime; | |
} | |
player.rigidbody.velocity = right*moveSpeed; | |
}else if (horizontal < 0){ | |
if( probe.counter > 0 ){ | |
cameraCollisionBox.transform.position += right*moveSpeed*Time.deltaTime; | |
} | |
player.rigidbody.velocity = -right*moveSpeed; | |
} | |
if( vertical!=0 || horizontal!=0 ){ | |
float rotate = Mathf.Atan2 (player.rigidbody.velocity.x, player.rigidbody.velocity.z); | |
player.transform.rotation = Quaternion.Euler (0, rotate / Mathf.PI * 180, 0); | |
}else{ | |
player.rigidbody.velocity = Vector3.zero; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment