2
0

SteamVR_TestThrow.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. namespace Valve.VR.Extras
  5. {
  6. [RequireComponent(typeof(SteamVR_TrackedObject))]
  7. public class SteamVR_TestThrow : MonoBehaviour
  8. {
  9. public GameObject prefab;
  10. public Rigidbody attachPoint;
  11. public SteamVR_Action_Boolean spawn = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("InteractUI");
  12. SteamVR_Behaviour_Pose trackedObj;
  13. FixedJoint joint;
  14. private void Awake()
  15. {
  16. trackedObj = GetComponent<SteamVR_Behaviour_Pose>();
  17. }
  18. private void FixedUpdate()
  19. {
  20. if (joint == null && spawn.GetStateDown(trackedObj.inputSource))
  21. {
  22. GameObject go = GameObject.Instantiate(prefab);
  23. go.transform.position = attachPoint.transform.position;
  24. joint = go.AddComponent<FixedJoint>();
  25. joint.connectedBody = attachPoint;
  26. }
  27. else if (joint != null && spawn.GetStateUp(trackedObj.inputSource))
  28. {
  29. GameObject go = joint.gameObject;
  30. Rigidbody rigidbody = go.GetComponent<Rigidbody>();
  31. Object.DestroyImmediate(joint);
  32. joint = null;
  33. Object.Destroy(go, 15.0f);
  34. // We should probably apply the offset between trackedObj.transform.position
  35. // and device.transform.pos to insert into the physics sim at the correct
  36. // location, however, we would then want to predict ahead the visual representation
  37. // by the same amount we are predicting our render poses.
  38. Transform origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
  39. if (origin != null)
  40. {
  41. rigidbody.velocity = origin.TransformVector(trackedObj.GetVelocity());
  42. rigidbody.angularVelocity = origin.TransformVector(trackedObj.GetAngularVelocity());
  43. }
  44. else
  45. {
  46. rigidbody.velocity = trackedObj.GetVelocity();
  47. rigidbody.angularVelocity = trackedObj.GetAngularVelocity();
  48. }
  49. rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
  50. }
  51. }
  52. }
  53. }