Advertisement
Guest User

Moving

a guest
Dec 11th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class MovingUpDown : MonoBehaviour {
  5.  
  6.     public float minY, maxY;
  7.     public float speed = 1;
  8.     public float waitTime = 1;
  9.  
  10.     delegate bool Condition();
  11.  
  12.     IEnumerator Moving ()
  13.     {
  14.         int direction = 1;
  15.         while (true)
  16.         {
  17.             Condition isMoving;
  18.             if (direction == 1)
  19.                 isMoving = () => { return transform.position.y < maxY; };
  20.             else
  21.                 isMoving = () => { return transform.position.y > minY; };
  22.  
  23.             while (isMoving())
  24.             {
  25.                 yield return null;
  26.                 transform.Translate(Vector3.up * direction * speed * Time.deltaTime);
  27.             }
  28.  
  29.             yield return new WaitForSeconds(waitTime);
  30.             direction *= -1;
  31.         }
  32.     }
  33.  
  34.     void Start ()
  35.     {
  36.         StartCoroutine(Moving());
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement