- Home /
Vive controller interaction with gameobject (C#)
I've been trying to script an interaction with a game object so that when the controller and gameobject colliders are touching and the trigger is pressed, a snowball will spawn. Also whilst the trigger is held down the snowball will stay attached to the controller. This script is attached to both controllers.
The game object is a rigidbody which is not kinematic with a box collider which is a trigger. The controllers also have sphere colliders which are triggers (I have also tried it with the controllers not being triggers, the game object not being a trigger and both not being triggers - none of which do anything). Nothing happens when the controller touches the game object, both with the trigger pressed and without. Can anyone help fix this please?
public class ControllerInteraction : MonoBehaviour {
SteamVR_Controller.Device device;
SteamVR_TrackedObject trackedobj;
public GameObject Snowball;
Rigidbody sbrb;
void Awake () {
trackedobj = GetComponent<SteamVR_TrackedObject>() ;
sbrb = Snowball.GetComponent<Rigidbody>();
}
void OnTriggerEnter(Collider col){
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
Instantiate(Snowball, gameObject.transform);
sbrb.isKinematic = true;
Snowball.gameObject.transform.SetParent(gameObject.transform);
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
sbrb.isKinematic = false;
Snowball.gameObject.transform.SetParent(null);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612125412im_/https://answers.unity.com/themes/thub/images/avi.jpg)