Advertisement
Guest User

Frustration

a guest
Dec 12th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MoveSquares : MonoBehaviour {
  5.  
  6.     public GameObject leftSquare;
  7.     public GameObject rightSquare;
  8.  
  9.     public float speed;
  10.  
  11.     public float topPosition;
  12.     public float bottomPosition;
  13.  
  14.     public bool leftGoingDown = true;
  15.     public bool transitioning = false;
  16.     public bool lifted = false;
  17.     public bool firstMove = true;
  18.  
  19.     public bool coroutineRunning = false;
  20.  
  21.     void Start () {
  22.         leftSquare = GameObject.Find("leftsquare");
  23.         rightSquare = GameObject.Find("rightsquare");
  24.     }
  25.  
  26.     void Update () {
  27.         if (!coroutineRunning) {
  28.             StartCoroutine(Movement());
  29.         }
  30.     }
  31.  
  32.     public IEnumerator Movement() {
  33.         coroutineRunning = true;
  34.         if (Input.GetButton("Vertical") && !transitioning) {
  35.             if (firstMove) {
  36.                 if (Input.GetAxis("Vertical") > 0) {
  37.                     leftGoingDown = false;
  38.                 }
  39.                 firstMove = false;
  40.             }
  41.             //flip direction
  42.             if (Input.GetAxis("Vertical") > 0) {
  43.                 if (lifted && leftGoingDown) {
  44.                     leftGoingDown = false;
  45.                     lifted = false;
  46.                 }
  47.             }
  48.             else if (Input.GetAxis("Vertical") < 0) {
  49.                 if (lifted && !leftGoingDown) {
  50.                     leftGoingDown = true;
  51.                     lifted = false;
  52.                 }
  53.             }
  54.             //movement
  55.             if (leftGoingDown) {
  56.                 leftSquare.transform.position += new Vector3(0, -1, 0) * speed * Time.deltaTime;
  57.                 rightSquare.transform.position += new Vector3(0, 1, 0) * speed * Time.deltaTime;
  58.             }
  59.             else if (!leftGoingDown) {
  60.                 leftSquare.transform.position += new Vector3(0, 1, 0) * speed * Time.deltaTime;
  61.                 rightSquare.transform.position += new Vector3(0, -1, 0) * speed * Time.deltaTime;
  62.             }
  63.             //transitions
  64.             if (leftSquare.transform.position.y <= bottomPosition) {
  65.                 leftSquare.transform.position = new Vector3(leftSquare.transform.position.x,
  66.                                                             bottomPosition,
  67.                                                             leftSquare.transform.position.z);
  68.                 rightSquare.transform.position = new Vector3(rightSquare.transform.position.x,
  69.                                                              topPosition,
  70.                                                              rightSquare.transform.position.z);
  71.                 leftGoingDown = false;
  72.                 transitioning = true;
  73.             }
  74.             if (leftSquare.transform.position.y >= topPosition) {
  75.                 leftSquare.transform.position = new Vector3(leftSquare.transform.position.x,
  76.                                                             topPosition,
  77.                                                             leftSquare.transform.position.z);
  78.                 rightSquare.transform.position = new Vector3(rightSquare.transform.position.x,
  79.                                                              bottomPosition,
  80.                                                              rightSquare.transform.position.z);
  81.                 leftGoingDown = true;
  82.                 transitioning = true;
  83.             }
  84.         }
  85.         if (Input.GetButtonUp("Vertical")) {
  86.             lifted = true;
  87.             transitioning = false;
  88.             Debug.Log ("get button up");
  89.         }
  90.         if (Input.GetButton("Horizontal")) {
  91.             //nothing happens
  92.             //this is the joke
  93.         }
  94.         coroutineRunning = false;
  95.         yield return null;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement