Skip to content

Commit

Permalink
feat(Controls): add spring lever
Browse files Browse the repository at this point in the history
A new lever 3D control that adds spring loading to
the functionality of VRTK_Lever
  • Loading branch information
cameronoltmann committed Sep 18, 2016
1 parent 2e9443c commit 7d91807
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Assets/VRTK/Scripts/Controls/3D/VRTK_Lever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public enum LeverDirection
[Tooltip("The increments in which lever values can change.")]
public float stepSize = 1f;

protected HingeJoint hj;

private Rigidbody rb;
private VRTK_InteractableObject io;
private HingeJoint hj;
private bool hjCreated = false;

protected override void InitRequiredComponents()
Expand Down
71 changes: 71 additions & 0 deletions Assets/VRTK/Scripts/Controls/3D/VRTK_Spring_Lever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Lever|Controls3D|0071
namespace VRTK
{
using UnityEngine;

/// <summary>
/// This script extends VRTK_Lever to add spring force toward whichever end of the lever's range it is closest to.
/// </summary>
/// <remarks>
/// The script will instantiate the required Rigidbody, Interactable and HingeJoint components automatically in case they do not exist yet. The joint is very tricky to setup automatically though and will only work in straight forward cases. If there are any issues, then create the HingeJoint component manually and configure it as needed.
/// </remarks>
public class VRTK_Spring_Lever : VRTK_Lever
{

[Tooltip("Strength of the spring force that will be applied toward either end of the lever's range.")]
public float springStrength = 10;

private bool wasTowardZero = true;
private bool towardZero;

/// <summary>
/// Override the original InitRequiredComponents() to add
/// handling of spring forces on the hingeJoint
/// </summary>
protected override void InitRequiredComponents()
{
base.InitRequiredComponents();
if (!hj.useSpring)
{
// If useSpring isn't set, the hingeJoint was probably automatically added - fix it
hj.useSpring = true;
JointSpring spring = hj.spring;
spring.spring = springStrength;
spring.targetPosition = minAngle;
hj.spring = spring;
}
else
{
// If useSpring is set, the hingeJoint was manually added - respect its settings
springStrength = hj.spring.spring;
}
}

/// <summary>
/// Adjust spring force during HandleUpdate()
/// </summary>
protected override void HandleUpdate()
{
base.HandleUpdate();
ApplySpringForce();
}

/// <summary>
/// Check which direction the lever needs to be pushed in and
/// switch spring direction as necessary
/// </summary>
private void ApplySpringForce()
{
// get normalized value
towardZero = (GetNormalizedValue() <= 50);
if (towardZero != wasTowardZero)
{
JointSpring spring = hj.spring;
spring.targetPosition = (towardZero) ? minAngle : maxAngle;
hj.spring = spring;
wasTowardZero = towardZero;
}
}

}
}
12 changes: 12 additions & 0 deletions Assets/VRTK/Scripts/Controls/3D/VRTK_Spring_Lever.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d91807

Please sign in to comment.