-
Notifications
You must be signed in to change notification settings - Fork 47
/
AlignPlayer.cs
89 lines (66 loc) · 2.15 KB
/
AlignPlayer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Meta Platforms, Inc. and affiliates.
// This code is licensed under the MIT license (see LICENSE for details).
using System.Collections;
using UnityEngine;
using Sampleton = SampleController; // only transitional
public class AlignPlayer : MonoBehaviour
{
public static AlignPlayer Instance { get; private set; }
[SerializeField]
Transform player;
[SerializeField]
Transform playerHands;
SharedAnchor m_CurrentAlignmentAnchor;
Coroutine m_AlignCoroutine;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this);
}
}
public void SetAlignmentAnchor(SharedAnchor anchor)
{
if (m_AlignCoroutine != null)
{
StopCoroutine(m_AlignCoroutine);
m_AlignCoroutine = null;
}
Sampleton.Log($"{nameof(AlignPlayer)}: setting {anchor} as the alignment anchor...");
if (m_CurrentAlignmentAnchor)
{
Sampleton.Log($"{nameof(AlignPlayer)}: unset {m_CurrentAlignmentAnchor} as the alignment anchor.");
m_CurrentAlignmentAnchor.IsSelectedForAlign = false;
}
m_CurrentAlignmentAnchor = null;
if (player)
{
player.SetPositionAndRotation(default, Quaternion.identity);
}
if (!anchor || !player)
return;
m_AlignCoroutine = StartCoroutine(RealignRoutine(anchor));
}
IEnumerator RealignRoutine(SharedAnchor anchor)
{
yield return null;
var anchorTransform = anchor.transform;
player.position = anchorTransform.InverseTransformPoint(Vector3.zero);
player.eulerAngles = new Vector3(0, -anchorTransform.eulerAngles.y, 0);
if (playerHands)
{
playerHands.SetLocalPositionAndRotation(
-player.position,
Quaternion.Inverse(player.rotation)
);
}
m_CurrentAlignmentAnchor = anchor;
anchor.IsSelectedForAlign = true;
Sampleton.Log($"{nameof(AlignPlayer)}: finished alignment -> {anchor}");
m_AlignCoroutine = null;
}
}