LockToPoint.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Valve.VR.InteractionSystem.Sample
  5. {
  6. public class LockToPoint : MonoBehaviour
  7. {
  8. public Transform snapTo;
  9. public Transform snapToDynamic;
  10. private Rigidbody body;
  11. public float snapTime = 2;
  12. private float dropTimer;
  13. private Interactable interactable;
  14. public bool SnapDynamic;
  15. private void Start()
  16. {
  17. interactable = GetComponent<Interactable>();
  18. body = GetComponent<Rigidbody>();
  19. SnapDynamic = false;
  20. }
  21. private void FixedUpdate()
  22. {
  23. bool used = false;
  24. if (interactable != null)
  25. used = interactable.attachedToHand;
  26. if (used)
  27. {
  28. body.isKinematic = false;
  29. dropTimer = -1;
  30. }
  31. else
  32. {
  33. dropTimer += Time.deltaTime / (snapTime / 2);
  34. body.isKinematic = dropTimer > 1;
  35. if (dropTimer > 1)
  36. {
  37. //transform.parent = snapTo;
  38. transform.position = SnapDynamic ? snapToDynamic.position : snapTo.position;
  39. transform.rotation = SnapDynamic ? snapToDynamic.rotation : snapTo.rotation;
  40. }
  41. else
  42. {
  43. float t = Mathf.Pow(35, dropTimer);
  44. body.velocity = Vector3.Lerp(body.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
  45. if (body.useGravity)
  46. body.AddForce(-Physics.gravity);
  47. transform.position = Vector3.Lerp(transform.position, SnapDynamic ? snapToDynamic.position : snapTo.position, Time.fixedDeltaTime * t * 3);
  48. transform.rotation = Quaternion.Slerp(transform.rotation, SnapDynamic ? snapToDynamic.rotation : snapTo.rotation, Time.fixedDeltaTime * t * 2);
  49. }
  50. }
  51. }
  52. }
  53. }