Last active
April 17, 2019 16:46
-
-
Save brismithSFHS/1ba4f1b6fe45e8237d187d23256e9d9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using System.Collections; | |
public class PlayerController : MonoBehaviour { | |
public float speed; | |
public int numCubes; | |
public Text countText; | |
public Text winText; | |
public Text oopsText; | |
private Rigidbody rb; | |
private int count; | |
private ArrayList collected = new ArrayList(); | |
private bool jumped; | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody>(); | |
count = 0; | |
SetCountText(); | |
winText.text = ""; | |
oopsText.text = ""; | |
} | |
void FixedUpdate () | |
{ | |
float moveHorizontal = Input.GetAxis("Horizontal"); | |
float moveVertical = Input.GetAxis("Vertical"); | |
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); | |
rb.AddForce(movement * speed); | |
if (this.transform.position.y <= -20) | |
{ | |
this.transform.position = new Vector3 (0, 5, 0); | |
count = 0; | |
foreach (Collider thing in collected) { | |
thing.gameObject.SetActive(true); | |
} | |
collected = new ArrayList(); | |
SetCountText (); | |
oopsText.text = "Oops! Try Again!"; | |
winText.text = ""; | |
} | |
if(Input.GetKeyDown("space") && !jumped) | |
{ | |
Vector3 bounce = new Vector3 (0.0f, speed * 20, 0.0f); | |
rb.AddForce(bounce); | |
jumped = true; | |
} | |
} | |
void OnTriggerEnter(Collider other) | |
{ | |
if (other.gameObject.CompareTag("Pick Up")) | |
{ | |
other.gameObject.SetActive(false); | |
count++; | |
SetCountText(); | |
collected.Add (other); // | |
} | |
oopsText.text = ""; | |
} | |
void SetCountText () | |
{ | |
countText.text = "Count: " + count.ToString (); | |
if (count >= numCubes) | |
{ | |
winText.text = "You have won!"; | |
} | |
} | |
void OnCollisionStay() | |
{ | |
jumped = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment