Advertisement
Zuvi

Untitled

Jun 6th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlatformMovement : MonoBehaviour
  6. {
  7. public bool isVertical = true;
  8. public float start;
  9. public float end;
  10. public float speed = 4;
  11.  
  12. public float waitingLenght = .5f;
  13. private float waitingTime = 0f;
  14. private bool isWaiting = false;
  15. private bool movingToEnd = true;
  16.  
  17.  
  18. // Update is called once per frame
  19. void Start()
  20. {
  21.  
  22. }
  23. void Update()
  24. {
  25. if (isVertical)
  26. {
  27. if (movingToEnd && !isWaiting)
  28. {
  29. transform.Translate(Vector2.right * speed * Time.deltaTime);
  30. }
  31. if (!movingToEnd && !isWaiting)
  32. {
  33. transform.Translate(Vector2.left * speed * Time.deltaTime);
  34. }
  35. if (transform.position.x >= end && movingToEnd == true)
  36. {
  37. movingToEnd = false;
  38. isWaiting = true;
  39. waitingTime = waitingLenght;
  40. transform.position=(new Vector2(end, transform.position.y));
  41. }
  42. if (transform.position.x <= start && movingToEnd == false)
  43. {
  44. movingToEnd = true;
  45. isWaiting = true;
  46. waitingTime = waitingLenght;
  47. transform.position=(new Vector2(start, transform.position.y));
  48. }
  49. }
  50. else
  51. {
  52. if (movingToEnd && !isWaiting)
  53. {
  54. transform.Translate(Vector2.up * speed * Time.deltaTime);
  55. }
  56. if (!movingToEnd && !isWaiting)
  57. {
  58. transform.Translate(Vector2.down * speed * Time.deltaTime);
  59. }
  60. if (transform.position.y >= end && movingToEnd == true)
  61. {
  62. movingToEnd = false;
  63. isWaiting = true;
  64. waitingTime = waitingLenght;
  65. transform.position=(new Vector2(transform.position.x, end));
  66. }
  67. if (transform.position.y <= start && movingToEnd == false)
  68. {
  69. movingToEnd = true;
  70. isWaiting = true;
  71. waitingTime = waitingLenght;
  72. transform.position=(new Vector2(transform.position.x, start));
  73. }
  74. }
  75. if (isWaiting)
  76. {
  77. waitingTime -= Time.deltaTime;
  78. if (waitingTime <= 0)
  79. isWaiting = false;
  80. }
  81. }
  82. void Wait(float seconds)
  83. {
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement