Skip to content

Commit befc10c

Browse files
authored
Update TetheredPlacement.cs (#10566)
1 parent 741f9aa commit befc10c

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

Assets/MRTK/Examples/Demos/HandTracking/Scripts/TetheredPlacement.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using UnityEngine;
5+
using UnityEngine.Serialization;
56

67
namespace Microsoft.MixedReality.Toolkit.Examples.Demos
78
{
@@ -11,26 +12,40 @@ namespace Microsoft.MixedReality.Toolkit.Examples.Demos
1112
[AddComponentMenu("Scripts/MRTK/Examples/TetheredPlacement")]
1213
public class TetheredPlacement : MonoBehaviour
1314
{
14-
[Tooltip("The distance from the GameObject's spawn position at which will trigger a respawn ")]
15-
public float DistanceThreshold = 20.0f;
15+
[SerializeField, Tooltip("The distance from the GameObject's spawn position at which will trigger a respawn."), FormerlySerializedAs("DistanceThreshold")]
16+
private float distanceThreshold = 20.0f;
1617

17-
private Vector3 RespawnPoint;
18-
private Quaternion RespawnOrientation;
18+
/// <summary>
19+
/// The distance from the GameObject's spawn position at which will trigger a respawn.
20+
/// </summary>
21+
/// <remarks>Also updates a local cache of this value squared for performant distance checking.</remarks>
22+
public float DistanceThreshold
23+
{
24+
get => distanceThreshold;
25+
set
26+
{
27+
distanceThreshold = value;
28+
distanceThresholdSquared = distanceThreshold * distanceThreshold;
29+
}
30+
}
1931

32+
private Vector3 localRespawnPosition;
33+
private Quaternion localRespawnRotation;
2034
private Rigidbody rigidBody;
35+
private float distanceThresholdSquared;
2136

2237
private void Start()
2338
{
2439
rigidBody = GetComponent<Rigidbody>();
25-
2640
LockSpawnPoint();
41+
distanceThresholdSquared = distanceThreshold * distanceThreshold;
2742
}
2843

2944
private void LateUpdate()
3045
{
31-
float distanceSqr = (RespawnPoint - this.transform.position).sqrMagnitude;
46+
float distanceSqr = (localRespawnPosition - transform.localPosition).sqrMagnitude;
3247

33-
if (distanceSqr > DistanceThreshold * DistanceThreshold)
48+
if (distanceSqr > distanceThresholdSquared)
3449
{
3550
// Reset any velocity from falling or moving when respawning to original location
3651
if (rigidBody != null)
@@ -39,15 +54,18 @@ private void LateUpdate()
3954
rigidBody.angularVelocity = Vector3.zero;
4055
}
4156

42-
this.transform.SetPositionAndRotation(RespawnPoint, RespawnOrientation);
57+
transform.localPosition = localRespawnPosition;
58+
transform.localRotation = localRespawnRotation;
4359
}
4460
}
4561

62+
/// <summary>
63+
/// Updates the local respawn pose to the objects current pose.
64+
/// </summary>
4665
public void LockSpawnPoint()
4766
{
48-
RespawnPoint = this.transform.position;
49-
RespawnOrientation = this.transform.rotation;
67+
localRespawnPosition = transform.localPosition;
68+
localRespawnRotation = transform.localRotation;
5069
}
5170
}
52-
53-
}
71+
}

0 commit comments

Comments
 (0)