Last active
January 25, 2016 12:58
-
-
Save DuckOfDoom/14dfff66e5cf217e55af to your computer and use it in GitHub Desktop.
Unity3d FaceOnCamera
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 System; | |
using UnityEngine; | |
/// <summary> | |
/// Поворачивает объект Z-осью на камеру. Также можно указать, какая ось объекта будет смотреть вверх. | |
/// </summary> | |
public class FaceOnCamera : MonoBehaviour | |
{ | |
[SerializeField] private bool _faceOnEditorCamera; | |
[SerializeField] private Axis _axisUp = Axis.Y; | |
[SerializeField] private Rotation _lockRotation = Rotation.None; | |
[SerializeField] private bool _reverse; | |
[SerializeField] private bool _drawGizmos = true; | |
private Camera _camera; | |
public bool FaceOnEditorCamera { get { return _faceOnEditorCamera; } set { _faceOnEditorCamera = value; } } | |
public Axis AxisUp { get { return _axisUp; } set { _axisUp = value; } } | |
public Rotation LockRotation { get { return _lockRotation; } set { _lockRotation = value; } } | |
public bool Reverse { get { return _reverse; } set { _reverse = value; } } | |
public bool DrawGizmos { get { return _drawGizmos; } set { _drawGizmos = value; } } | |
private void LateUpdate() | |
{ | |
if (_faceOnEditorCamera) | |
return; | |
if (_camera == null) | |
_camera = Camera.main; | |
FaceOn(_camera); | |
} | |
public void FaceOn(Camera cam) | |
{ | |
if (cam == null) | |
return; | |
var targetPosition = new Vector3(LockRotation == Rotation.Vertical ? transform.position.x : cam.transform.position.x, | |
LockRotation == Rotation.Horizontal ? transform.position.y : cam.transform.position.y, | |
cam.transform.position.z); | |
var targetOrientation = cam.transform.rotation * GetAxis(); | |
transform.LookAt(targetPosition, targetOrientation); | |
if (_reverse) | |
transform.Rotate(0, 180, 0, Space.Self); | |
} | |
private Vector3 GetAxis() | |
{ | |
if (LockRotation != Rotation.None) | |
return Vector3.up; | |
switch (_axisUp) | |
{ | |
case Axis.Y: | |
return Vector3.up; | |
case Axis.YReversed: | |
return Vector3.down; | |
case Axis.XReversed: | |
return Vector3.left; | |
case Axis.X: | |
return Vector3.right; | |
default: | |
return Vector3.up; | |
} | |
} | |
private void OnDrawGizmos() | |
{ | |
if (!DrawGizmos) | |
return; | |
Gizmos.color = Color.red; | |
Gizmos.DrawRay(transform.position, transform.right); | |
Gizmos.color = Color.green; | |
Gizmos.DrawRay(transform.position, transform.up); | |
Gizmos.color = Color.blue; | |
Gizmos.DrawRay(transform.position, transform.forward); | |
} | |
public enum Axis | |
{ | |
Y, | |
YReversed, | |
X, | |
XReversed | |
}; | |
public enum Rotation | |
{ | |
None, | |
Horizontal, | |
Vertical | |
}; | |
} |
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 UnityEditor; | |
using UnityEngine; | |
[CustomEditor(typeof(FaceOnCamera))] | |
public class FaceOnCameraEditor : Editor | |
{ | |
private FaceOnCamera _target; | |
private void Awake() | |
{ | |
_target = (FaceOnCamera) target; | |
} | |
public override void OnInspectorGUI() | |
{ | |
_target.FaceOnEditorCamera = GUILayout.Toggle(_target.FaceOnEditorCamera, " - Face On Editor Camera"); | |
_target.Reverse = GUILayout.Toggle(_target.Reverse, " - Reverse"); | |
_target.LockRotation = (FaceOnCamera.Rotation) EditorGUILayout.EnumPopup("Lock Rotation: ", _target.LockRotation); | |
if(_target.LockRotation == FaceOnCamera.Rotation.None) | |
_target.AxisUp = (FaceOnCamera.Axis) EditorGUILayout.EnumPopup("Axis Up:", _target.AxisUp); | |
GUILayout.Space(10f); | |
_target.DrawGizmos = GUILayout.Toggle(_target.DrawGizmos, " - Draw Gizmos"); | |
} | |
public void OnSceneGUI() | |
{ | |
if (_target.FaceOnEditorCamera) | |
_target.FaceOn(SceneView.lastActiveSceneView.camera); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment