IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_TestThrow.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 using UnityEngine;
3 using System.Collections;
4 
5 [RequireComponent(typeof(SteamVR_TrackedObject))]
6 public class SteamVR_TestThrow : MonoBehaviour
7 {
8  public GameObject prefab;
9  public Rigidbody attachPoint;
10 
11  SteamVR_TrackedObject trackedObj;
12  FixedJoint joint;
13 
14  void Awake()
15  {
16  trackedObj = GetComponent<SteamVR_TrackedObject>();
17  }
18 
19  void FixedUpdate()
20  {
21  var device = SteamVR_Controller.Input((int)trackedObj.index);
22  if (joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
23  {
24  var go = GameObject.Instantiate(prefab);
25  go.transform.position = attachPoint.transform.position;
26 
27  joint = go.AddComponent<FixedJoint>();
28  joint.connectedBody = attachPoint;
29  }
30  else if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
31  {
32  var go = joint.gameObject;
33  var rigidbody = go.GetComponent<Rigidbody>();
34  Object.DestroyImmediate(joint);
35  joint = null;
36  Object.Destroy(go, 15.0f);
37 
38  // We should probably apply the offset between trackedObj.transform.position
39  // and device.transform.pos to insert into the physics sim at the correct
40  // location, however, we would then want to predict ahead the visual representation
41  // by the same amount we are predicting our render poses.
42 
43  var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
44  if (origin != null)
45  {
46  rigidbody.velocity = origin.TransformVector(device.velocity);
47  rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
48  }
49  else
50  {
51  rigidbody.velocity = device.velocity;
52  rigidbody.angularVelocity = device.angularVelocity;
53  }
54 
55  rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
56  }
57  }
58 }