Created
January 9, 2018 10:00
-
-
Save youten/cbacbe1094546498c199235c3e98f8a5 to your computer and use it in GitHub Desktop.
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// ref. https://review-of-my-life.blogspot.jp/2017/11/throw-objects-and-move-aroud-with-laser-in-vr.html | |
// generate ingot and throw | |
public class IngotGenerator : MonoBehaviour { | |
public Transform positionTarget; | |
private static string INGOT_PATH = "Prefabs/ingot"; | |
private GameObject ingotGenerated; | |
void Update () { | |
SteamVR_Controller.Device device = SteamVR_Controller.Input ((int)GetComponent<SteamVR_TrackedObject> ().index); | |
if (device.GetPressDown (SteamVR_Controller.ButtonMask.Trigger)) { | |
// instantiate ingot at attached object's Transform | |
if (positionTarget) { | |
ingotGenerated = Instantiate ((GameObject)Resources.Load (INGOT_PATH), positionTarget.position, gameObject.transform.rotation * Quaternion.Euler(90,90,0)); | |
} else { | |
ingotGenerated = Instantiate ((GameObject)Resources.Load (INGOT_PATH), gameObject.transform.position, gameObject.transform.rotation * Quaternion.Euler(90,90,0)); | |
} | |
// grab ingot | |
addFixedJoint ().connectedBody = ingotGenerated.GetComponent<Rigidbody> (); | |
} else if (device.GetPressUp (SteamVR_Controller.ButtonMask.Trigger)) { | |
// release ingot | |
FixedJoint joint = gameObject.GetComponent<FixedJoint> (); | |
if (joint != null) { | |
joint.connectedBody = null; | |
Destroy (joint); | |
Rigidbody rigidbody = ingotGenerated.GetComponent<Rigidbody> (); | |
rigidbody.velocity = device.velocity; | |
rigidbody.angularVelocity = device.angularVelocity; | |
} | |
if (ingotGenerated != null) { | |
Destroy (ingotGenerated, 3.0f); | |
} | |
ingotGenerated = null; | |
} | |
} | |
private FixedJoint addFixedJoint() { | |
FixedJoint joint = gameObject.AddComponent<FixedJoint> (); | |
joint.breakForce = 20000; | |
joint.breakTorque = 20000; | |
return joint; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment