- Home /
SteamVR Interactable Scripts, enable/disable
I have been trying to toggle SteamVR's interactions scripts off and on with another script using an OnTriggerEnter event. I have had success with other scripts but the interaction scripts seem a little different. They can't be toggled off and on in the inspector so I wonder if I can't use .enabled = true/false. I would be very happy if anyone had any info on how these scripts differ!
using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace Valve.VR.InteractionSystem {
public class Cuttable : MonoBehaviour
{
public GameObject branch;
private void OnTriggerEnter(Collider other)
{
branch.transform.parent = null;
Rigidbody rb = GetComponentInParent<Rigidbody>();
rb.constraints = RigidbodyConstraints.None;
branch.GetComponent<Throwable>().enabled = true;
GetComponent<TestScript>().enabled = true;
}
// Use this for initialization
void Start()
{
branch.GetComponent<Throwable>().enabled = false;
branch.GetComponent<Interactable>().enabled = false;
branch.GetComponent<VelocityEstimator>().enabled = false;
GetComponent<TestScript>().enabled = false;
}
}
}
As you can see'm trying to toggle off a script called 'Throwing', 'Interactable', and 'VelocityEstimator' on start and then after the trigger event turn 'Throwing' back on. The test script was seeing if the method worked, and it did. The interaction scripts are apart of the parent object called branch which I have made public and dragged properly into the inspector.
[1]: /storage/temp/116909-throwablescript.png
Answer by hschulte · Feb 06, 2019 at 10:58 AM
I had the same problem and could solve it by deriving from Throwable and adding some checks to the respective function calls.
public class ThrowableOnSteroids : Throwable
{
protected void Start()
{
//To have an enabled checkbox
}
protected override void OnAttachedToHand(Hand hand)
{
if(enabled) base.OnAttachedToHand(hand);
}
protected override void OnHandHoverBegin(Hand hand)
{
if(enabled) base.OnHandHoverBegin(hand);
}
protected override void HandHoverUpdate(Hand hand)
{
if(enabled) base.HandHoverUpdate(hand);
}
}
@hschulte Thank you so much for sharing! Are you dropping this script on in addition to Throwable or did you modify the actual throwable script?
You're welcome :)
As this derives from Throwable, you replace your original Throwable script by this. It is important to us to keep third party scripts untouched to prevent a big hassle when updating the S$$anonymous$$mVR plugin.
This worked well: it also needs also a reference at the top
using Valve.VR.InteractionSystem;
You are my personal Hero! Thank You - I've learned a lot on this example.
Answer by Thaoren · May 01, 2019 at 02:50 PM
I have figured this out! Valve intends us to add the IgnoreHovering
component to GameObjects with a Collider
we want to have the Hand hovering system to ignore.
(The relevant Hand interaction code can been seen in Hand.cs:518-526)
For just a single GameObject with a single collider:
GameObject toEnableOrDisableHovering;
// To enable hovering:
Destroy( toEnableOrDisableHovering.GetComponent<IgnoreHovering>() );
// To disable hovering:
toEnableOrDisableHovering.AddComponent<IgnoreHovering>();
In this code snippet I disable hovering in the entire hierarchy of one set of GameObjects and enable it for another set:
GameObject[] toDisableHovering = *array of GameObjects*;
GameObject[] toEnableHovering = *array of GameObjects*;
// Enable hovering by ensuring any IgnoreHovering components have been removed
foreach( IgnoreHovering ignoreHovering in toEnableHovering.GetComponentsInChildren<IgnoreHovering>() ) {
Destroy( ignoreHovering );
}
// Disable hovering by adding IgnoreHover components to the GameObject's with Colliders
foreach( Collider coll in toDisableHovering.GetComponentsInChildren<Collider>() ) {
coll.gameObject.AddComponent<IgnoreHovering>();
}
Answer by esillen · Jan 09, 2019 at 10:10 AM
For your question about the checkbox, the answer is that Unity leaves it out if your script doesn't have any per frame update methods, see this answer: http://answers.unity.com/answers/7933/view.html
I'm not sure what you're trying to do but it could be that you want to "turn off all code" in these scripts. That's unfortunately not how .enable
works. It will still run most of the code, just not the update methods.
Another example of this (dealing with .enable
and OnTriggerEnter()
) is answered here: https://forum.unity.com/threads/should-ontriggerenter-be-getting-called-even-when-the-script-is-disabled.240391/
Answer by clint205 · Feb 01, 2019 at 07:29 PM
@RustleJ Did you find a way to "turn off and on" a SteamVR interactable at runtime via script? Really am needing that now :)
As this is a year old I'm sure you have an answer but if any one else comes by, look at the answer by @Thaoren
namespace Valve.VR.InteractionSystem
{
public class SetHoverState : $$anonymous$$onoBehaviour
{
public GameObject toDisableHovering;
public GameObject toEnableHovering;
void Start()
{
}
public void EnableHovering() {
// Enable hovering by ensuring any IgnoreHovering components have been removed
foreach (IgnoreHovering ignoreHovering in toEnableHovering.GetComponentsInChildren<IgnoreHovering>())
{
Destroy(ignoreHovering);
}
}
public void DisableHovering()
{
// Disable hovering by adding IgnoreHover components to the GameObject's with Colliders
foreach (Collider coll in toDisableHovering.GetComponentsInChildren<Collider>())
{
coll.gameObject.AddComponent<IgnoreHovering>();
}
}
}
}
Then use a Unity Event or some other means to call the enable or disable hovering function in the above script.
Your answer
Follow this Question
Related Questions
How do I switch levels when the user grabs an object in SteamVR? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to enable teleport area lock in Steam VR? 1 Answer
SteamVR error 109 issues 1 Answer