Created
March 16, 2018 22:20
-
-
Save brismithSFHS/a5b9760f8ff7616285d4b4be1ca6b5ce to your computer and use it in GitHub Desktop.
Modified Unity Tutorial scripts including: A camera controller script for Unity Roll-a-ball that lets you rotate the camera, a wall mover, and the player controller to go with them
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 System.Collections; | |
public class CameraController : MonoBehaviour { | |
public GameObject player; | |
public static int rotPosition = 0; | |
private Vector3 offset; | |
private bool rotating = false; | |
//adding new stuff from: https://answers.unity.com/questions/655896/rotating-a-camera-90-degrees-around-an-object.html | |
private float targetAngle = 0; | |
const float rotationAmount = 10.0f; | |
// Update is called once per frame | |
void Update() | |
{ | |
// Trigger functions if Rotate is requested | |
if (Input.GetKeyDown("z")) | |
{ | |
targetAngle -= 90.0f; | |
} else if (Input.GetKeyDown("x")) { | |
targetAngle += 90.0f; | |
} | |
if(targetAngle !=0) | |
{ | |
rotating = true; | |
Rotate(); | |
} | |
} | |
protected void Rotate() | |
{ | |
if (targetAngle>0) | |
{ | |
transform.RotateAround(player.transform.position, Vector3.up, -rotationAmount); | |
targetAngle -= rotationAmount; | |
} | |
else if(targetAngle <0) | |
{ | |
transform.RotateAround(player.transform.position, Vector3.up, rotationAmount); | |
targetAngle += rotationAmount; | |
} | |
if (targetAngle == 0) | |
{ | |
rotating = false; | |
} | |
} | |
//end of modified new stuff | |
// Use this for initialization | |
void Start () | |
{ | |
offset = transform.position - player.transform.position; | |
} | |
// Update is called once per frame | |
void LateUpdate () | |
{ | |
if (Input.GetKeyDown ("z")) | |
{ | |
rotPosition++; | |
if (rotPosition > 3) | |
rotPosition = 0; | |
} | |
else if (Input.GetKeyDown ("x")) | |
{ | |
rotPosition--; | |
if (rotPosition < 0) | |
rotPosition = 3; | |
} | |
switch (rotPosition) | |
{ | |
case 0: | |
offset = new Vector3(0, 7, -10); | |
break; | |
case 1: | |
offset = new Vector3(-10, 7, 0); | |
break; | |
case 2: | |
offset = new Vector3(0, 7, 10); | |
break; | |
case 3: | |
offset = new Vector3(10, 7, 0); | |
break; | |
} | |
if (rotating == false) | |
{ | |
transform.position = player.transform.position + offset; | |
} | |
} | |
} |
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MoveWall : MonoBehaviour { | |
public Rigidbody rb; | |
public float moveAmount; | |
// Use this for initialization | |
void Start () { | |
rb = GetComponent<Rigidbody> (); | |
} | |
// Update is called once per frame | |
void Update () { | |
rb.position = new Vector3(rb.position.x, rb.position.y + moveAmount, rb.position.z); | |
if (rb.position.y > 2 || rb.position.y < -2) | |
moveAmount *= -1; | |
} | |
} |
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"); //x | |
float moveVertical = Input.GetAxis("Vertical"); //y | |
Vector3 movement = new Vector3(); | |
int rotationSwitch = CameraController.rotPosition; | |
switch (rotationSwitch) | |
{ | |
case 0: | |
movement = new Vector3(moveHorizontal, 0.0f, moveVertical); | |
break; | |
case 1: | |
movement = new Vector3(moveVertical, 0.0f, -1 * moveHorizontal); | |
break; | |
case 2: | |
movement = new Vector3(moveHorizontal * -1, 0.0f, moveVertical * -1); | |
break; | |
case 3: | |
movement = new Vector3(moveVertical * -1, 0.0f, moveHorizontal); | |
break; | |
} | |
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