Created
May 26, 2015 14:28
-
-
Save romainPechot/013dcd6f1a2632eaee78 to your computer and use it in GitHub Desktop.
Look At Monobehaviour for camera in cinematic or cut scene
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 SC_PlayerCameraCutScene : MonoBehaviour | |
{ | |
#region Static | |
// CONST | |
private const float lerpLookAtSpeedOn = 1f; | |
private const float lerpLookAtSpeedOff = 1f; | |
private const float lerpPositionSpeedOn = 4f; | |
private const float lerpPositionSpeedOff = 4f; | |
// SINGLETON | |
private static SC_PlayerCameraCutScene _instance; | |
// target look at | |
private static Transform _T_targetLookAt; | |
private static Vector3 _V3_smoothTargetLookAt = Vector3.forward; | |
// target position | |
private static Transform _T_targetPosition; | |
private static Vector3 _V3_smoothTargetPosition = Vector3.zero; | |
public static bool IS_ACTIVE{get{return (IS_ACTIVE_LOOK_AT || IS_ACTIVE_POSITION);}} | |
public static bool IS_ACTIVE_LOOK_AT{get{return _T_targetLookAt != null;}} | |
public static bool IS_ACTIVE_POSITION{get{return _T_targetPosition != null;}} | |
public static void Reset(){SetLookAt(null);} | |
public static void SetLookAt(Transform _T_newTarget) | |
{ | |
if(_T_newTarget != _T_targetLookAt) | |
{ | |
// set new target | |
_T_targetLookAt = _T_newTarget; | |
// reset position look at | |
_V3_smoothTargetLookAt = _instance.transform.TransformPoint(Vector3.forward * 10f); | |
} | |
}// SetLookAt() | |
public static void SetTargetPosition(Transform _T_newTarget) | |
{ | |
if(_T_newTarget != _T_targetPosition) | |
{ | |
// set target | |
_T_targetPosition = _T_newTarget; | |
// save current position | |
_V3_smoothTargetPosition = _instance.transform.position; | |
} | |
}// SetTargetPosition() | |
#endregion | |
#region Dynamic | |
private void Awake() | |
{ | |
_instance = this; | |
}// Awake() | |
private void LateUpdate() | |
{ | |
// look at ? | |
if(IS_ACTIVE_LOOK_AT) | |
{ | |
// lerp | |
_V3_smoothTargetLookAt = Vector3.Lerp(_V3_smoothTargetLookAt, _T_targetLookAt.position, TimeManager.DELTA_TIME * lerpLookAtSpeedOn); | |
// apply | |
transform.LookAt(_V3_smoothTargetLookAt); | |
} | |
else | |
{ | |
//Reset rotation | |
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, TimeManager.DELTA_TIME * lerpLookAtSpeedOff); | |
} | |
// move at ? | |
if(IS_ACTIVE_POSITION) | |
{ | |
// lerp | |
_V3_smoothTargetPosition = Vector3.Lerp(_V3_smoothTargetPosition, _T_targetPosition.position, TimeManager.DELTA_TIME * lerpPositionSpeedOn); | |
// apply | |
transform.position = _V3_smoothTargetPosition; | |
} | |
else | |
{ | |
// reset position | |
transform.localPosition = Vector3.Lerp(transform.localPosition, Vector3.zero, TimeManager.DELTA_TIME * lerpPositionSpeedOff); | |
} | |
}// LateUpdate | |
#endregion | |
}// SC_PlayerCameraCutScene : MonoBehaviour |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment