Last active
February 10, 2016 17:44
-
-
Save jjhesk/77b535a28666f496890a to your computer and use it in GitHub Desktop.
Character Controller Logic
This file contains 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
/// <summary> | |
/// UnityTutorials homepage: https://github.com/jm991/UnityTutorials | |
/// This software is provided 'as-is', without any express or implied | |
/// warranty. In no event will the authors be held liable for any damages | |
/// arising from the use of this software. | |
/// | |
/// Permission is granted to anyone to use this software for any purpose, | |
/// and to alter it and redistribute it freely, subject to the following restrictions: | |
/// | |
/// 1. The origin of this software must not be misrepresented; you must not | |
/// claim that you wrote the original software. If you use this software | |
/// in a product, an acknowledgment in the product documentation would be | |
/// appreciated but is not required. | |
/// 2. Altered source versions must be plainly marked as such, and must not be | |
/// misrepresented as being the original software. | |
/// 3. This notice may not be removed or altered from any source distribution. | |
/// </summary> | |
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// #DESCRIPTION OF CLASS# | |
/// </summary> | |
public class CharacterControllerLogic : MonoBehaviour | |
{ | |
#region Variables (private) | |
// Inspector serialized | |
[SerializeField] | |
private Animator | |
animator; | |
[SerializeField] | |
private FantasticCamera | |
gamecam; | |
[SerializeField] | |
private float | |
rotationDegreePerSecond = 120f; | |
[SerializeField] | |
private float | |
directionSpeed = 1.5f; | |
[SerializeField] | |
private float | |
directionDampTime = 0.25f; | |
[SerializeField] | |
private float | |
speedDampTime = 0.05f; | |
[SerializeField] | |
private float | |
fovDampTime = 3f; | |
[SerializeField] | |
private float | |
jumpMultiplier = 1f; | |
[SerializeField] | |
private CapsuleCollider | |
capCollider; | |
[SerializeField] | |
private float | |
jumpDist = 1f; | |
[SerializeField] | |
private bool | |
sprint = true; | |
// Private global only | |
private float leftX = 0f; | |
private float leftY = 0f; | |
private AnimatorStateInfo stateInfo; | |
private AnimatorTransitionInfo transInfo; | |
private float speed = 0f; | |
private float direction = 0f; | |
private float charAngle = 0f; | |
private const float SPRINT_SPEED = 2.0f; | |
private const float SPRINT_FOV = 75.0f; | |
private const float NORMAL_FOV = 60.0f; | |
private float capsuleHeight; | |
// Hashes | |
private int m_LocomotionId = 0; | |
private int m_LocomotionPivotLId = 0; | |
private int m_LocomotionPivotRId = 0; | |
private int m_LocomotionPivotLTransId = 0; | |
private int m_LocomotionPivotRTransId = 0; | |
#endregion | |
#region Properties (public) | |
public Animator Animator { | |
get { | |
return this.animator; | |
} | |
} | |
public float Speed { | |
get { | |
return this.speed; | |
} | |
} | |
public float LocomotionThreshold { get { return 0.2f; } } | |
public FantasticCamera FansCam { | |
set { | |
this.gamecam = value; | |
} | |
} | |
#endregion | |
#region Unity event functions | |
/// <summary> | |
/// Use this for initialization. | |
/// </summary> | |
void Start () | |
{ | |
//animator = GetComponent<Animator>(); | |
//capCollider = GetComponent<CapsuleCollider>(); | |
//capsuleHeight = capCollider.height; | |
//if(animator.layerCount >= 2) | |
//{ | |
// animator.SetLayerWeight(1, 1); | |
//} | |
// Hash all animation names for performance | |
//m_LocomotionId = Animator.StringToHash("Base Layer.Locomotion"); | |
//m_LocomotionPivotLId = Animator.StringToHash("Base Layer.LocomotionPivotL"); | |
//m_LocomotionPivotRId = Animator.StringToHash("Base Layer.LocomotionPivotR"); | |
//m_LocomotionPivotLTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotL"); | |
//m_LocomotionPivotRTransId = Animator.StringToHash("Base Layer.Locomotion -> Base Layer.LocomotionPivotR"); | |
} | |
/// <summary> | |
/// Update is called once per frame. | |
/// </summary> | |
void Update () | |
{ | |
if (gamecam == null) | |
return; | |
if (gamecam.CamState != FantasticCamera.CamStates.FirstPerson) { | |
charAngle = 0f; | |
direction = 0f; | |
float charSpeed = 0f; | |
// Translate controls stick coordinates into world/cam/character space | |
StickToWorldspace (this.transform, gamecam.transform, ref direction, ref charSpeed, ref charAngle, IsInPivot ()); | |
// Press B to sprint | |
// | |
//if (Input.GetButton ("Sprint")) { | |
if (sprint) { | |
speed = Mathf.Lerp (speed, SPRINT_SPEED, Time.deltaTime); | |
gamecam.camera.fieldOfView = Mathf.Lerp (gamecam.camera.fieldOfView, SPRINT_FOV, fovDampTime * Time.deltaTime); | |
} else { | |
speed = charSpeed; | |
gamecam.camera.fieldOfView = Mathf.Lerp (gamecam.camera.fieldOfView, NORMAL_FOV, fovDampTime * Time.deltaTime); | |
} | |
} | |
// if (animator && gamecam.CamState != FantasticCamera.CamStates.FirstPerson) { | |
// //stateInfo = animator.GetCurrentAnimatorStateInfo (0); | |
// //transInfo = animator.GetAnimatorTransitionInfo (0); | |
// // Press A to jump | |
// //if (Input.GetButton ("Jump")) { | |
// //animator.SetBool("Jump", true); | |
// //} else { | |
// //animator.SetBool("Jump", false); | |
// //} | |
// // Pull values from controller/keyboard | |
// //leftX = Input.GetAxis ("Horizontal"); | |
// //leftY = Input.GetAxis ("Vertical"); | |
// | |
// charAngle = 0f; | |
// direction = 0f; | |
// float charSpeed = 0f; | |
// | |
// // Translate controls stick coordinates into world/cam/character space | |
// StickToWorldspace (this.transform, gamecam.transform, ref direction, ref charSpeed, ref charAngle, IsInPivot ()); | |
// | |
// // Press B to sprint | |
// // | |
// bool sprint = false; | |
// //if (Input.GetButton ("Sprint")) { | |
// if (sprint) { | |
// speed = Mathf.Lerp (speed, SPRINT_SPEED, Time.deltaTime); | |
// gamecam.camera.fieldOfView = Mathf.Lerp (gamecam.camera.fieldOfView, SPRINT_FOV, fovDampTime * Time.deltaTime); | |
// } else { | |
// speed = charSpeed; | |
// gamecam.camera.fieldOfView = Mathf.Lerp (gamecam.camera.fieldOfView, NORMAL_FOV, fovDampTime * Time.deltaTime); | |
// } | |
// | |
// //animator.SetFloat("Speed", speed, speedDampTime, Time.deltaTime); | |
// //animator.SetFloat("Direction", direction, directionDampTime, Time.deltaTime); | |
// | |
// if (speed > LocomotionThreshold) { // Dead zone | |
// if (!IsInPivot ()) { | |
// Animator.SetFloat ("Angle", charAngle); | |
// } | |
// } | |
// if (speed < LocomotionThreshold && Mathf.Abs (leftX) < 0.05f) { // Dead zone | |
// //animator.SetFloat("Direction", 0f); | |
// //animator.SetFloat("Angle", 0f); | |
// } | |
// } | |
} | |
/// <summary> | |
/// Any code that moves the character needs to be checked against physics | |
/// </summary> | |
void FixedUpdate () | |
{ | |
// Rotate character model if stick is tilted right or left, but only if character is moving in that direction | |
// if (IsInLocomotion() && gamecam.CamState != FantasticCamera.CamStates.Free && !IsInPivot() && ((direction >= 0 && leftX >= 0) || (direction < 0 && leftX < 0))) | |
// { | |
// Vector3 rotationAmount = Vector3.Lerp(Vector3.zero, new Vector3(0f, rotationDegreePerSecond * (leftX < 0f ? -1f : 1f), 0f), Mathf.Abs(leftX)); | |
// Quaternion deltaRotation = Quaternion.Euler(rotationAmount * Time.deltaTime); | |
// this.transform.rotation = (this.transform.rotation * deltaRotation); | |
// } | |
// if (IsInJump()) | |
// { | |
// float oldY = transform.position.y; | |
// //transform.Translate(Vector3.up * jumpMultiplier * animator.GetFloat("JumpCurve")); | |
// if (IsInLocomotionJump()) | |
// { | |
// transform.Translate(Vector3.forward * Time.deltaTime * jumpDist); | |
// } | |
// capCollider.height = capsuleHeight + (animator.GetFloat("CapsuleCurve") * 0.5f); | |
// if (gamecam.CamState != FantasticCamera.CamStates.Free) | |
// { | |
// gamecam.ParentRig.Translate(Vector3.up * (transform.position.y - oldY)); | |
// } | |
// } | |
} | |
/// <summary> | |
/// Debugging information should be put here. | |
/// </summary> | |
void OnDrawGizmos () | |
{ | |
} | |
#endregion | |
#region Methods | |
public bool IsInJump () | |
{ | |
return (IsInIdleJump () || IsInLocomotionJump ()); | |
} | |
public bool IsInIdleJump () | |
{ | |
return false; | |
//animator.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.IdleJump"); | |
} | |
public bool IsInLocomotionJump () | |
{ | |
return false;//animator.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.LocomotionJump"); | |
} | |
public bool IsInPivot () | |
{ | |
return false; | |
// | |
// stateInfo.nameHash == m_LocomotionPivotLId || | |
// stateInfo.nameHash == m_LocomotionPivotRId || | |
// transInfo.nameHash == m_LocomotionPivotLTransId || | |
// transInfo.nameHash == m_LocomotionPivotRTransId; | |
} | |
public void triggerFocusMain () | |
{ | |
gamecam.triggerFocusMain (this, this.gameObject.transform); | |
} | |
public bool IsInLocomotion () | |
{ | |
return stateInfo.nameHash == m_LocomotionId; | |
} | |
public void StickToWorldspace (Transform root, Transform camera, ref float directionOut, ref float speedOut, ref float angleOut, bool isPivoting) | |
{ | |
Vector3 rootDirection = root.forward; | |
Vector3 stickDirection = new Vector3 (leftX, 0, leftY); | |
speedOut = stickDirection.sqrMagnitude; | |
// Get camera rotation | |
Vector3 CameraDirection = camera.forward; | |
CameraDirection.y = 0.0f; // kill Y | |
Quaternion referentialShift = Quaternion.FromToRotation (Vector3.forward, Vector3.Normalize (CameraDirection)); | |
// Convert joystick input in Worldspace coordinates | |
Vector3 moveDirection = referentialShift * stickDirection; | |
Vector3 axisSign = Vector3.Cross (moveDirection, rootDirection); | |
Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 2f, root.position.z), moveDirection, Color.green); | |
Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 2f, root.position.z), rootDirection, Color.magenta); | |
Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 2f, root.position.z), stickDirection, Color.blue); | |
Debug.DrawRay (new Vector3 (root.position.x, root.position.y + 2.5f, root.position.z), axisSign, Color.red); | |
float angleRootToMove = Vector3.Angle (rootDirection, moveDirection) * (axisSign.y >= 0 ? -1f : 1f); | |
if (!isPivoting) { | |
angleOut = angleRootToMove; | |
} | |
angleRootToMove /= 180f; | |
directionOut = angleRootToMove * directionSpeed; | |
} | |
#endregion Methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment