Created
May 26, 2015 14:44
-
-
Save aberloni/c8cb9675540f47d099ef to your computer and use it in GitHub Desktop.
UNITY - Transform will progressively look at another transform
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 ProgressiveLookAt : MonoBehaviour | |
{ | |
Transform _origin; // itself | |
//LOOK AT | |
public float lookSpeed = 1f; // with target ref | |
public Transform target; | |
Vector3 originOrientation = Vector3.forward; | |
//READ ONLY STATES | |
bool IS_ACTIVE{get{return IS_ACTIVE_LOOK_AT;}} | |
bool IS_ACTIVE_LOOK_AT{get{return target != null;}} | |
private void Start() | |
{ | |
_origin = transform; | |
SetLookAt(target); | |
} | |
private void LateUpdate() | |
{ | |
// look at ? | |
if(IS_ACTIVE_LOOK_AT) | |
{ | |
// lerp | |
originOrientation = Vector3.Lerp(originOrientation, target.position, Time.deltaTime * lookSpeed); | |
// apply | |
transform.LookAt(originOrientation); | |
} | |
} | |
public void SetLookAt(Transform _T_newTarget) | |
{ | |
if(_T_newTarget != target) | |
{ | |
// set new target | |
target = _T_newTarget; | |
// reset position look at | |
originOrientation = _origin.transform.TransformPoint(Vector3.forward * 10f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment