Hello everyone,
I got my Vive 5 days ago and have been enjoying it thoroughly and decided I’d give development a shot with Unity.
I’m currently trying to write my own interaction script, I know there are free scripts out there but I’d like to learn something in the process and I like to know what my script does.
Anyway, I’m currently kind of stuck and need a hint.
I can register trigger presses, I can register when my controller is colliding with a rigid body and I can, sort of, interact with it.
What I mean with that is, I’m using MoveTowards to move the interactable item to the controller. The interact ble item, however, will slowly move downwards as if gravity is slowly pulling it down.
That’s problem 1.
Problem 2 is, when I release the trigger, sometimes it’ll let the interactable item go, and other times it’ll keep holding it.
Personally I feel the problem is with my void OnTriggerExit(Collider collider) method.
ViveWirelessController.cs:
using UnityEngine;
using System.Collections;
publicclassViveWirelessController : MonoBehaviour {
// Shorten name
privateulong triggerButton = SteamVR_Controller.ButtonMask.Trigger;
// Create an InteractItem object?
private InteractItem interactItem;
// Retrieve the index of the controller. (Can be different for every game launch).
private SteamVR_Controller.Device controller {
get {
return SteamVR_Controller.Input((int)GetComponent<SteamVR_TrackedObject>().index);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () { // FixedUpdate?
// Check to see if the player is pressing the triggerButton and if there is currently an interactItem.
if (controller.GetPressDown(triggerButton) && interactItem !=null) {
// Debug.Log("Trigger Button Pressed");
// Start the interaction for the current controller.
interactItem.BeginInteraction(this);
}
// Check to see if the player let go of the triggerButton and if there is currently an interactItem.
if (controller.GetPressUp(triggerButton) && interactItem !=null) {
// Debug.Log("Trigger Button Released");
// End the interaction for this controller.
interactItem.EndInteraction(this);
}
}
void OnTriggerEnter(Collider collider) {
// Debug.Log("OnTriggerEnter");
// Set the interactItem to the item the controller is currently colliding with.
interactItem = collider.GetComponent<InteractItem>();
}
void OnTriggerExit(Collider collider) {
// Debug.Log("OnTriggerExit");
// Check to see if the trigger is being pressed.
if (controller.GetPressUp(triggerButton)) {
// If the trigger is not being pressed, allow interactItem to be set to null;
interactItem =null;
// Debug.Log("interactItem is null");
}
}
}
InteractItem.cs
using UnityEngine;
using System.Collections;
publicclassInteractItem : MonoBehaviour {
private ViveWirelessController currentController;
privatefloat velocity =1000f;
privatebool currentlyInteracting;
// Use this for initialization
void Start () { // Can be changed to Awake for now I think?
currentlyInteracting =false;
}
// Update is called once per frame
void FixedUpdate () {
if (currentlyInteracting) {
// Took this from the MoveTowards documention on the Unity website.
float step = velocity * Time.fixedDeltaTime;
// Move the InteractItem towards the currentController's position.
this.transform.position = Vector3.MoveTowards(this.transform.position, currentController.transform.position, step);
// Set currentController as the new parent of the this InteractItem.
this.transform.SetParent(currentController.transform, true); // I'm not sure if I'm properly applying SetParent.
}
}
publicvoid BeginInteraction(ViveWirelessController controller) {
// Debug.Log("BeginInteraction");
// set the currentController to the controller calling BeginInteraction
currentController = controller;
// Set currentlyInteracting to true to check if controller is referencing to an existing object.
currentlyInteracting =true;
}
publicvoid EndInteraction(ViveWirelessController controller) {
// Debug.Log("EndInteraction");
// Set currentController to null. There is no interaction anymore, remove the reference to the controller.
currentController =null;
// Set currentlyInteracting to false to check if controller is referencing to an existing object.
currentlyInteracting =false;
}
}
Code looks a little messy, not sure why the website decided to format it like that but oh well.
If someone could point me in the right direction or give me a hint. (Maybe more efficient method that I could use instead) that’d be great.
Thanks in advance.