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