Skip to content

Commit 2c2ee2c

Browse files
Merge pull request #549 from cadenasgmbh/HandDraggableLockRotation
Added possibility to lock the objects rotation while dragging it
2 parents 3bf1ef2 + f72c2ab commit 2c2ee2c

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

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

Lines changed: 21 additions & 13 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;
@@ -31,12 +31,16 @@ public class HandDraggable : MonoBehaviour,
3131

3232
[Tooltip("Scale by which hand movement in z is multipled to move the dragged object.")]
3333
public float DistanceScale = 2f;
34+
35+
public enum RotationModeEnum
36+
{
37+
Default,
38+
LockObjectRotation,
39+
OrientTowardUser,
40+
OrientTowardUserAndKeepUpright
41+
}
3442

35-
[Tooltip("Should the object be kept upright as it is being dragged?")]
36-
public bool IsKeepUpright = false;
37-
38-
[Tooltip("Should the object be oriented towards the user as it is being dragged?")]
39-
public bool IsOrientTowardsUser = true;
43+
public RotationModeEnum RotationMode = RotationModeEnum.Default;
4044

4145
public bool IsDraggingEnabled = true;
4246

@@ -194,11 +198,15 @@ private void UpdateDragging()
194198

195199
draggingPosition = pivotPosition + (targetDirection * targetDistance);
196200

197-
if (IsOrientTowardsUser)
201+
if (RotationMode == RotationModeEnum.OrientTowardUser || RotationMode == RotationModeEnum.OrientTowardUserAndKeepUpright)
198202
{
199203
draggingRotation = Quaternion.LookRotation(HostTransform.position - pivotPosition);
200204
}
201-
else
205+
else if (RotationMode == RotationModeEnum.LockObjectRotation)
206+
{
207+
draggingRotation = HostTransform.rotation;
208+
}
209+
else // RotationModeEnum.Default
202210
{
203211
Vector3 objForward = mainCamera.transform.TransformDirection(objRefForward); // in world space
204212
Vector3 objUp = mainCamera.transform.TransformDirection(objRefUp); // in world space
@@ -207,12 +215,12 @@ private void UpdateDragging()
207215

208216
// Apply Final Position
209217
HostTransform.position = draggingPosition + mainCamera.transform.TransformDirection(objRefGrabPoint);
218+
// Apply Final Rotation
210219
HostTransform.rotation = draggingRotation;
211-
212-
if (IsKeepUpright)
213-
{
214-
Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up);
215-
HostTransform.rotation = upRotation * HostTransform.rotation;
220+
if (RotationMode == RotationModeEnum.OrientTowardUserAndKeepUpright)
221+
{
222+
Quaternion upRotation = Quaternion.FromToRotation(HostTransform.up, Vector3.up);
223+
HostTransform.rotation = upRotation * HostTransform.rotation;
216224
}
217225
}
218226

0 commit comments

Comments
 (0)