-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraFacingSprite.cs
57 lines (50 loc) · 1.41 KB
/
CameraFacingSprite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using UnityEngine;
using UnityEditor;
// Thanks to Neil Carter (NCarter) for the initial script!
// http://wiki.unity3d.com/index.php?title=CameraFacingBillboard
namespace MeshEdit
{
/// <summary>
/// Attach this to any GameObject with a sprite that should always face the active camera
/// </summary>
[ExecuteAlways]
public class CameraFacingSprite : MonoBehaviour
{
[SerializeField]
private bool _play = false; // Are we in play mode (true) or edit mode (false)
[SerializeField]
private Camera faceCam = null;
void Awake()
{
UpdateState();
RotateToFace(_play);
}
void OnRenderObject()
{
RotateToFace(_play);
}
void UpdateState()
{
_play = Application.isPlaying;
if (_play)
{
faceCam = Camera.main; // In-game Main camera
}
else
{
faceCam = SceneView.lastActiveSceneView.camera; // Editor camera
}
}
void RotateToFace(bool ingame)
{
if (faceCam != null)
{
transform.LookAt(transform.position + faceCam.transform.rotation * Vector3.forward, faceCam.transform.rotation * Vector3.up);
}
else
{
UpdateState();
}
}
}
}