LockToPoint.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. private Rigidbody body;
  10. public float snapTime = 2;
  11. private float dropTimer;
  12. private Interactable interactable;
  13. private void Start()
  14. {
  15. interactable = GetComponent<Interactable>();
  16. body = GetComponent<Rigidbody>();
  17. }
  18. private void FixedUpdate()
  19. {
  20. bool used = false;
  21. if (interactable != null)
  22. used = interactable.attachedToHand;
  23. if (used)
  24. {
  25. body.isKinematic = false;
  26. dropTimer = -1;
  27. }
  28. else
  29. {
  30. dropTimer += Time.deltaTime / (snapTime / 2);
  31. body.isKinematic = dropTimer > 1;
  32. if (dropTimer > 1)
  33. {
  34. //transform.parent = snapTo;
  35. transform.position = snapTo.position;
  36. transform.rotation = snapTo.rotation;
  37. }
  38. else
  39. {
  40. float t = Mathf.Pow(35, dropTimer);
  41. body.velocity = Vector3.Lerp(body.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
  42. if (body.useGravity)
  43. body.AddForce(-Physics.gravity);
  44. transform.position = Vector3.Lerp(transform.position, snapTo.position, Time.fixedDeltaTime * t * 3);
  45. transform.rotation = Quaternion.Slerp(transform.rotation, snapTo.rotation, Time.fixedDeltaTime * t * 2);
  46. }
  47. }
  48. }
  49. }
  50. }