Last active
October 26, 2017 00:59
-
-
Save atakotestudios/9f21c0eba6b5d6d668c2d623193e8686 to your computer and use it in GitHub Desktop.
Script for controller in 2 hr intro session 5/9/2017
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
//How to Use: Attach this script to Controller(left) and/or Controller(right) to enable object pickup and throwing. | |
//Controller(left/right) need trigger collider components. Objects you want to interact with need the "Throwable" tag. | |
[RequireComponent(typeof(Collider))] | |
[RequireComponent(typeof(Rigidbody))] | |
public class GrabThrowFromHands : MonoBehaviour { | |
private SteamVR_TrackedObject trackedObj; | |
private SteamVR_Controller.Device device; | |
public float throwForce = 1.5f; | |
// Use this for initialization | |
void Start () { | |
trackedObj = GetComponent<SteamVR_TrackedObject>(); | |
} | |
// Update is called once per frame | |
void Update () { | |
device = SteamVR_Controller.Input((int)trackedObj.index); | |
} | |
void OnTriggerStay(Collider col) | |
{ | |
if (col.gameObject.CompareTag("projectile")) | |
{ | |
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) | |
{ | |
//Multi Throwing | |
col.transform.SetParent(null); | |
Rigidbody rigidBody = col.GetComponent<Rigidbody>(); | |
rigidBody.isKinematic = false; | |
rigidBody.velocity = device.velocity * throwForce; | |
rigidBody.angularVelocity = device.angularVelocity; | |
} | |
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) | |
{ | |
col.GetComponent<Rigidbody>().isKinematic = true; | |
col.transform.SetParent(gameObject.transform); | |
device.TriggerHapticPulse(2000); | |
//If you want the object to move to the center of hand when grabbed | |
//col.gameObject.transform.position = gameObject.transform.position; | |
} | |
} | |
} | |
} | |
using UnityEngine; | |
using System.Collections; | |
//How to Use: Attach this script to Controller(left) and/or Controller(right) to enable object pickup and throwing. | |
//Controller(left/right) need trigger collider components. Objects you want to interact with need the "Throwable" tag. | |
[RequireComponent(typeof(Collider))] | |
[RequireComponent(typeof(Rigidbody))] | |
public class GrabThrowFromHands : MonoBehaviour { | |
private SteamVR_TrackedObject trackedObj; | |
private SteamVR_Controller.Device device; | |
public float throwForce = 1.5f; | |
// Use this for initialization | |
void Start () { | |
trackedObj = GetComponent<SteamVR_TrackedObject>(); | |
} | |
// Update is called once per frame | |
void Update () { | |
device = SteamVR_Controller.Input((int)trackedObj.index); | |
} | |
void OnTriggerStay(Collider col) | |
{ | |
if (col.gameObject.CompareTag("projectile")) | |
{ | |
if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger)) | |
{ | |
//Multi Throwing | |
col.transform.SetParent(null); | |
Rigidbody rigidBody = col.GetComponent<Rigidbody>(); | |
rigidBody.isKinematic = false; | |
rigidBody.velocity = device.velocity * throwForce; | |
rigidBody.angularVelocity = device.angularVelocity; | |
} | |
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) | |
{ | |
col.GetComponent<Rigidbody>().isKinematic = true; | |
col.transform.SetParent(gameObject.transform); | |
device.TriggerHapticPulse(2000); | |
//If you want the object to move to the center of hand when grabbed | |
//col.gameObject.transform.position = gameObject.transform.position; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment