doorBehavior.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class doorBehavior : MonoBehaviour
  5. {
  6. public bool close;
  7. public bool moving;
  8. public float speed;
  9. private const float closeCenter = 1.5f;
  10. private const float openCenter = 4.5f;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. close = true;
  15. speed = 1f;
  16. }
  17. void Move(float diff)
  18. {
  19. if (diff > speed * 0.04f)
  20. {
  21. transform.Translate(Vector3.down * speed * Time.deltaTime);
  22. }
  23. else if (diff < -speed * 0.04f)
  24. {
  25. transform.Translate(Vector3.up * speed * Time.deltaTime);
  26. }
  27. else
  28. {
  29. moving = false;
  30. return;
  31. }
  32. moving = true;
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. if (close)
  38. {
  39. float diff = transform.position.y - closeCenter;
  40. Move(diff);
  41. }
  42. else
  43. {
  44. float diff = transform.position.y - openCenter;
  45. Move(diff);
  46. }
  47. }
  48. }