Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MoveSquares : MonoBehaviour {
- public GameObject leftSquare;
- public GameObject rightSquare;
- public float speed;
- public float topPosition;
- public float bottomPosition;
- public bool leftGoingDown = true;
- public bool transitioning = false;
- public bool lifted = false;
- public bool firstMove = true;
- public bool coroutineRunning = false;
- void Start () {
- leftSquare = GameObject.Find("leftsquare");
- rightSquare = GameObject.Find("rightsquare");
- }
- void Update () {
- if (!coroutineRunning) {
- StartCoroutine(Movement());
- }
- }
- public IEnumerator Movement() {
- coroutineRunning = true;
- if (Input.GetButton("Vertical") && !transitioning) {
- if (firstMove) {
- if (Input.GetAxis("Vertical") > 0) {
- leftGoingDown = false;
- }
- firstMove = false;
- }
- //flip direction
- if (Input.GetAxis("Vertical") > 0) {
- if (lifted && leftGoingDown) {
- leftGoingDown = false;
- lifted = false;
- }
- }
- else if (Input.GetAxis("Vertical") < 0) {
- if (lifted && !leftGoingDown) {
- leftGoingDown = true;
- lifted = false;
- }
- }
- //movement
- if (leftGoingDown) {
- leftSquare.transform.position += new Vector3(0, -1, 0) * speed * Time.deltaTime;
- rightSquare.transform.position += new Vector3(0, 1, 0) * speed * Time.deltaTime;
- }
- else if (!leftGoingDown) {
- leftSquare.transform.position += new Vector3(0, 1, 0) * speed * Time.deltaTime;
- rightSquare.transform.position += new Vector3(0, -1, 0) * speed * Time.deltaTime;
- }
- //transitions
- if (leftSquare.transform.position.y <= bottomPosition) {
- leftSquare.transform.position = new Vector3(leftSquare.transform.position.x,
- bottomPosition,
- leftSquare.transform.position.z);
- rightSquare.transform.position = new Vector3(rightSquare.transform.position.x,
- topPosition,
- rightSquare.transform.position.z);
- leftGoingDown = false;
- transitioning = true;
- }
- if (leftSquare.transform.position.y >= topPosition) {
- leftSquare.transform.position = new Vector3(leftSquare.transform.position.x,
- topPosition,
- leftSquare.transform.position.z);
- rightSquare.transform.position = new Vector3(rightSquare.transform.position.x,
- bottomPosition,
- rightSquare.transform.position.z);
- leftGoingDown = true;
- transitioning = true;
- }
- }
- if (Input.GetButtonUp("Vertical")) {
- lifted = true;
- transitioning = false;
- Debug.Log ("get button up");
- }
- if (Input.GetButton("Horizontal")) {
- //nothing happens
- //this is the joke
- }
- coroutineRunning = false;
- yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement