-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBillboard.cs
76 lines (64 loc) · 2.19 KB
/
Billboard.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity
{
public enum PivotAxis
{
// Rotate about all axes.
Free,
// Rotate about an individual axis.
Y
}
/// <summary>
/// The Billboard class implements the behaviors needed to keep a GameObject oriented towards the user.
/// </summary>
public class Billboard : MonoBehaviour
{
/// <summary>
/// The axis about which the object will rotate.
/// </summary>
[Tooltip("Specifies the axis about which the object will rotate.")]
public PivotAxis PivotAxis = PivotAxis.Free;
[Tooltip("Specifies the target we will orient to. If no Target is specified the main camera will be used.")]
public Transform TargetTransform;
private void OnEnable()
{
if (TargetTransform == null)
{
TargetTransform = CameraCache.Main.transform;
}
Update();
}
/// <summary>
/// Keeps the object facing the camera.
/// </summary>
private void Update()
{
if (TargetTransform == null)
{
return;
}
// Get a Vector that points from the target to the main camera.
Vector3 directionToTarget = TargetTransform.position - transform.position;
// Adjust for the pivot axis.
switch (PivotAxis)
{
case PivotAxis.Y:
directionToTarget.y = 0.0f;
break;
case PivotAxis.Free:
default:
// No changes needed.
break;
}
// If we are right next to the camera the rotation is undefined.
if (directionToTarget.sqrMagnitude < 0.001f)
{
return;
}
// Calculate and apply the rotation required to reorient the object
transform.rotation = Quaternion.LookRotation(-directionToTarget);
}
}
}