Pickup object and throw it in HTC Vive
Hi! I'm a student at LBS Gothenburg, in Sweden, which is a Game Development high school (We do really basic shit). We just bought in a HTC Vive to the school and I'm one of the lucky few who gets to make a game with it. So my problem is, I'm trying to pickup an object (which is going to become a sword later on, but I'm first off trying to pickup an object). I have a simple script which lets me pickup the object, but the physics are screwed up. The object I'm picking up has a rigidbody, and when I pick it up I turn off gravity for the object. The problem, however, is the object is floating around in my hand (since it has no gravity), and if I collide with something else. The object goes flying of. And if I turn the object to Kinematic, the object goes through walls. The goal is to have something that is throwable, and sits in my hand perfectly.
Here's how my code is looking atm
using UnityEngine;
using System.Collections;
public class WandController : MonoBehaviour
{
private Valve.VR.EVRButtonId gripButton = Valve.VR.EVRButtonId.k_EButton_Grip;
private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
private SteamVR_Controller.Device controller { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
private SteamVR_TrackedObject trackedObj;
private GameObject pickup;
// Use this for initialization
void Start()
{
trackedObj = GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void Update()
{
if (controller == null)
{
Debug.Log("Controller not initialized");
return;
}
}
private void OnTriggerStay(Collider collider)
{
pickup = collider.gameObject;
if (controller.GetPressDown(gripButton) && pickup != null)
{
pickup.transform.parent = this.transform;
pickup.GetComponent<Rigidbody>().useGravity = false;
}
if (controller.GetPressUp(gripButton) && pickup != null)
{
pickup.transform.parent = null;
pickup.GetComponent<Rigidbody>().useGravity = true;
}
}
private void OnTriggerExit(Collider collider)
{
pickup.transform.parent = null;
pickup.GetComponent<Rigidbody>().useGravity = true;
}
}
Any tip would be awesome! Thanks!
-Fredric
I have a new Skype name : vipa3421 (there is somebody called Ananas (THATS $$anonymous$$E :D) but search for vipa3421 :))
Answer by FSK · Dec 13, 2016 at 02:07 AM
Hey, I'm from the Lessing Gymnasium Karlsruhe in Germany and I have the same problem as you. I'm creating a game for the Vive as a school project. Maybe you can add me on Skype : ananut_yt so we can work togehther on this problem (we can pickup and carry objects but when we throw things .... it sucks )
If you want to have my code for the WnadController add me on Skype :)
I couldn't find anything for ananut_yt, did you misspell? :P
Don't know but here is my email: skropavi@gmail.com So we can be in contact
Today I will create a new Skype Acc. I'll write you soon
Answer by Sciman101 · Mar 31, 2017 at 09:41 PM
You'll want to use a physics joint instead; im not positive how it works, but it'll get you the behavior you want