-
-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new lever 3D control that adds spring loading to the functionality of VRTK_Lever
- Loading branch information
1 parent
2e9443c
commit 7d91807
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.