-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceCamera.cs
More file actions
36 lines (31 loc) · 1.1 KB
/
FaceCamera.cs
File metadata and controls
36 lines (31 loc) · 1.1 KB
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
// Useful for Text Meshes that should face the camera.
//
// In some cases there seems to be a Unity bug where the text meshes end up in
// weird positions if it's not positioned at (0,0,0). In that case simply put it
// into an empty GameObject and use that empty GameObject for positioning.
using UnityEngine;
namespace uMMORPG
{
public class FaceCamera : MonoBehaviour
{
// Camera.main calls FindObjectWithTag each time. cache it!
Transform cam;
void Awake()
{
// find main camera
cam = Camera.main.transform;
// disable by default until visible
enabled = false;
}
// LateUpdate so that all camera updates are finished.
void LateUpdate()
{
transform.forward = cam.forward;
}
// copying transform.forward is relatively expensive and slows things down
// for large amounts of entities, so we only want to do it while the mesh
// is actually visible
void OnBecameVisible() { enabled = true; }
void OnBecameInvisible() { enabled = false; }
}
}