SteamVR_TestThrow.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(SteamVR_TrackedObject))]
  4. public class SteamVR_TestThrow : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. this.trackedObj = base.GetComponent<SteamVR_TrackedObject>();
  9. }
  10. private void FixedUpdate()
  11. {
  12. SteamVR_Controller.Device device = SteamVR_Controller.Input((int)this.trackedObj.index);
  13. if (this.joint == null && device.GetTouchDown(8589934592UL))
  14. {
  15. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.prefab);
  16. gameObject.transform.position = this.attachPoint.transform.position;
  17. this.joint = gameObject.AddComponent<FixedJoint>();
  18. this.joint.connectedBody = this.attachPoint;
  19. }
  20. else if (this.joint != null && device.GetTouchUp(8589934592UL))
  21. {
  22. GameObject gameObject2 = this.joint.gameObject;
  23. Rigidbody component = gameObject2.GetComponent<Rigidbody>();
  24. UnityEngine.Object.DestroyImmediate(this.joint);
  25. this.joint = null;
  26. UnityEngine.Object.Destroy(gameObject2, 15f);
  27. Transform transform = (!this.trackedObj.origin) ? this.trackedObj.transform.parent : this.trackedObj.origin;
  28. if (transform != null)
  29. {
  30. component.velocity = transform.TransformVector(device.velocity);
  31. component.angularVelocity = transform.TransformVector(device.angularVelocity);
  32. }
  33. else
  34. {
  35. component.velocity = device.velocity;
  36. component.angularVelocity = device.angularVelocity;
  37. }
  38. component.maxAngularVelocity = component.angularVelocity.magnitude;
  39. }
  40. }
  41. public GameObject prefab;
  42. public Rigidbody attachPoint;
  43. private SteamVR_TrackedObject trackedObj;
  44. private FixedJoint joint;
  45. }