Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.8 KiB

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