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.

56 lines
1.8 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. public class Openable_Door : VRTK_InteractableObject
  5. {
  6. public bool flipped = false;
  7. public bool rotated = false;
  8. private float sideFlip = -1;
  9. private float side = -1;
  10. private float smooth = 270.0f;
  11. private float doorOpenAngle = -90f;
  12. private bool open = false;
  13. private Vector3 defaultRotation;
  14. private Vector3 openRotation;
  15. public override void StartUsing(VRTK_InteractUse usingObject)
  16. {
  17. base.StartUsing(usingObject);
  18. SetDoorRotation(usingObject.transform.position);
  19. SetRotation();
  20. open = !open;
  21. }
  22. protected void Start()
  23. {
  24. defaultRotation = transform.eulerAngles;
  25. SetRotation();
  26. sideFlip = (flipped ? 1 : -1);
  27. }
  28. protected override void Update()
  29. {
  30. base.Update();
  31. if (open)
  32. {
  33. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(openRotation), Time.deltaTime * smooth);
  34. }
  35. else
  36. {
  37. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(defaultRotation), Time.deltaTime * smooth);
  38. }
  39. }
  40. private void SetRotation()
  41. {
  42. openRotation = new Vector3(defaultRotation.x, defaultRotation.y + (doorOpenAngle * (sideFlip * side)), defaultRotation.z);
  43. }
  44. private void SetDoorRotation(Vector3 interacterPosition)
  45. {
  46. side = ((rotated == false && interacterPosition.z > transform.position.z) || (rotated == true && interacterPosition.x > transform.position.x) ? -1 : 1);
  47. }
  48. }
  49. }