forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillboard.cs
More file actions
68 lines (58 loc) · 1.9 KB
/
Billboard.cs
File metadata and controls
68 lines (58 loc) · 1.9 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
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
// 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;
private void OnEnable()
{
Update();
}
/// <summary>
/// Keeps the object facing the camera.
/// </summary>
private void Update()
{
if (!Camera.main)
{
return;
}
// Get a Vector that points from the target to the main camera.
Vector3 directionToTarget = Camera.main.transform.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);
}
}
}