AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AxisSlider.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 namespace HoloToolkit.UI.Keyboard
7 {
11  public class AxisSlider : MonoBehaviour
12  {
13 
14  public enum EAxis
15  {
16  X,
17  Y,
18  Z
19  }
20 
21  public EAxis Axis = EAxis.X;
22 
23  private float currentPos;
24  private float slideVel;
25 
26  public float slideAccel = 5.25f;
27  public float slideFriction = 6f;
28  public float deadZone = 0.55f;
29  public float clampDistance = 300.0f;
30  public float bounce = 0.5f;
31 
32  [HideInInspector]
33  public Vector3 TargetPoint;
34 
35  private float GetAxis(Vector3 v)
36  {
37  switch (Axis)
38  {
39  case EAxis.X: return v.x;
40  case EAxis.Y: return v.y;
41  case EAxis.Z: return v.z;
42  }
43  return 0;
44  }
45 
46  private Vector3 SetAxis(Vector3 v, float f)
47  {
48  switch (Axis)
49  {
50  case EAxis.X: v.x = f; break;
51  case EAxis.Y: v.y = f; break;
52  case EAxis.Z: v.z = f; break;
53  }
54  return v;
55  }
56 
60  private void LateUpdate()
61  {
62  float targetP = GetAxis(TargetPoint);
63 
64  float dt = Time.deltaTime;
65  float delta = targetP - currentPos;
66 
67  // Accelerate left or right if outside of deadzone
68  if (Mathf.Abs(delta) > deadZone * deadZone)
69  {
70  slideVel += slideAccel * Mathf.Sign(delta) * dt;
71  }
72 
73  // Apply friction
74  slideVel -= slideVel * slideFriction * dt;
75 
76  // Apply velocity to position
77  currentPos += slideVel * dt;
78 
79  // Clamp to sides (bounce)
80  if (Mathf.Abs(currentPos) >= clampDistance)
81  {
82  slideVel *= -bounce;
83  currentPos = clampDistance * Mathf.Sign(currentPos);
84  }
85 
86  // Set position
87  transform.localPosition = SetAxis(transform.localPosition, currentPos);
88  }
89  }
90 }
A simple general use keyboard that is ideal for AR/VR applications.
Definition: Keyboard.cs:21
Axis slider is a script to lock a bar across a specific axis.
Definition: AxisSlider.cs:11