Skip to content

Commit 7670c96

Browse files
authored
Added possibility to lock the objects rotation while dragging it
When u drag a object via HandDraggable and move your head the rotation of the object changes. At least in my case I don't want that, it somehow feels unnatural, so I implemented a public flag to prevent this behaivour (lock the rotation of the object). Also I added a Header and Space for grouping the different rotation modes in the inspector (probably change the text there a bit?) And I grouped the handling of the rotation modes (upright, towards user, lock orientation) into an if-else if-else if construct. If nothing applies (when IsRotationLocked == true) the rotation of the object isn't changed at all (my desired behaivour). Any opinions?
1 parent 3f3cafb commit 7670c96

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Assets/HoloToolkit/Input/Scripts/Interactions/HandDraggable.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using UnityEngine;
@@ -32,12 +32,18 @@ public class HandDraggable : MonoBehaviour,
3232
[Tooltip("Scale by which hand movement in z is multipled to move the dragged object.")]
3333
public float DistanceScale = 2f;
3434

35+
[Header("Rotation Handling (only select one!)")]
3536
[Tooltip("Should the object be kept upright as it is being dragged?")]
3637
public bool IsKeepUpright = false;
3738

3839
[Tooltip("Should the object be oriented towards the user as it is being dragged?")]
3940
public bool IsOrientTowardsUser = true;
4041

42+
[Tooltip("Should the rotation of the object be locked?")]
43+
public bool LockRotation = false;
44+
45+
[Space(10)]
46+
4147
public bool IsDraggingEnabled = true;
4248

4349
private Camera mainCamera;
@@ -198,7 +204,12 @@ private void UpdateDragging()
198204
{
199205
draggingRotation = Quaternion.LookRotation(HostTransform.position - pivotPosition);
200206
}
201-
else
207+
else if (IsKeepUpright)
208+
{
209+
Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up);
210+
draggingRotation = upRotation * HostTransform.rotation; ;
211+
}
212+
else if (!IsRotationLocked)
202213
{
203214
Vector3 objForward = mainCamera.transform.TransformDirection(objRefForward); // in world space
204215
Vector3 objUp = mainCamera.transform.TransformDirection(objRefUp); // in world space
@@ -207,13 +218,8 @@ private void UpdateDragging()
207218

208219
// Apply Final Position
209220
HostTransform.position = draggingPosition + mainCamera.transform.TransformDirection(objRefGrabPoint);
221+
// Apply Final Rotation
210222
HostTransform.rotation = draggingRotation;
211-
212-
if (IsKeepUpright)
213-
{
214-
Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up);
215-
HostTransform.rotation = upRotation * HostTransform.rotation;
216-
}
217223
}
218224

219225
/// <summary>

0 commit comments

Comments
 (0)