Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Selections of the most important scripts from Going Up
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InsideCornerGear : MonoBehaviour {
//0, 270, 180, 90
public enum possibleGravityOptions { UP, RIGHT, DOWN, LEFT };
public possibleGravityOptions[] possibleGravityCaresAbout;
public bool isRotatingToNewAngle = false;
public possibleGravityOptions currentGravity;
public possibleGravityOptions goalGravity;
public float rotationSpeed = 1f;
public float percentageDone = 0f;
PlayerMove playerScript;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (isRotatingToNewAngle) {
RotateAllThingsToNewState();
}
else {
if (playerScript != null) {
playerScript.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1;
}
}
}
//UP, Right, Down, Left
//0 , 1 , 2 , 3
//0, 270 , 180 , 90
//0, -90 , -180, -270
public void RotateAllThingsToNewState() {
int gearAmountToRotate = 90;
if (goalGravity - currentGravity == 3 || goalGravity - currentGravity == -1) {
gearAmountToRotate *= -1;
}
//Movement through gravity to the left rotates the gear clockwise
//Player rotates COUNTER clockwise
float startingGearRotation = -90 * ((4 - (int)currentGravity) % 4);
//Rotation of the gear
this.transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, 0, startingGearRotation),
Quaternion.Euler(0, 0, startingGearRotation + gearAmountToRotate), percentageDone);
//Rotation of the player around the gear
//float playerEndRotation = 90 - Vector2.Angle(this.transform.position, playerScript.gameObject.transform.position);
float playerEndRotation = 90;
if (goalGravity - currentGravity == 3 || goalGravity - currentGravity == -1) {
playerEndRotation *= -1;
}
playerScript.gameObject.transform.RotateAround(gameObject.transform.position, Vector3.forward, playerEndRotation * Time.deltaTime / rotationSpeed);
//UP, Right, Down, Left
//0 , 1 , 2 , 3
//0, 270 , 180 , 90
//0, -90 , -180, -270
int playerAmountToRotate = 90;
if (goalGravity - currentGravity == -3 || goalGravity - currentGravity == 1) {
playerAmountToRotate *= -1;
}
float playerStartingRotation = 90 * ((4 - (int)currentGravity) % 4);
//Rotation of the player around itself
playerScript.gameObject.transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, 0, playerStartingRotation),
Quaternion.Euler(0, 0, playerStartingRotation + playerAmountToRotate), percentageDone);
percentageDone += Time.deltaTime / rotationSpeed;
if (percentageDone >= .9f) {
//Finish rotation and let player move again
isRotatingToNewAngle = false;
playerScript.manualGearRotationOverride = false;
//Lock rotation to the end state
this.transform.rotation = Quaternion.Euler(0, 0, startingGearRotation + gearAmountToRotate);
playerScript.gameObject.transform.rotation = Quaternion.Euler(0, 0, playerStartingRotation + playerAmountToRotate);
//Adjust the player's position so they don't immediately trigger the gear again
Vector3 playerFixPosition = new Vector3(0, 0, 0);
switch ((int)currentGravity) {
case 0: playerFixPosition = new Vector3(0, .3f, 0); break;
case 1: playerFixPosition = new Vector3(.3f, 0, 0); break;
case 2: playerFixPosition = new Vector3(0, -.3f, 0); break;
case 3: playerFixPosition = new Vector3(-.3f, 0, 0); break;
default: Debug.Log("Things are horribly broken in fixing player location"); break;
}
playerScript.gameObject.transform.position += playerFixPosition;
//Cleanup of variables
this.gameObject.GetComponent<CircleCollider2D>().enabled = true;
playerScript.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1;
playerScript = null;
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player") {
playerScript = other.gameObject.GetComponent<PlayerMove>();
if (!playerScript.canRaycastUpLogic) {
playerScript = null;
return;
}
//Determine how the player is supposed to rotate as well as how gravity is supposed to move
if ((int)possibleGravityCaresAbout[0] == (int)playerScript.currentGravity) {
currentGravity = possibleGravityCaresAbout[0];
goalGravity = possibleGravityCaresAbout[1];
}
else if ((int)possibleGravityCaresAbout[1] == (int)playerScript.currentGravity) {
currentGravity = possibleGravityCaresAbout[1];
goalGravity = possibleGravityCaresAbout[0];
}
else {
playerScript = null;
return;
}
//Rotate Gravity to the New Direction
if (goalGravity - currentGravity == 3) {
playerScript.GravityRotate(-1);
}
else {
playerScript.GravityRotate(goalGravity - currentGravity);
}
//Allow Rotation to Begin
other.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
playerScript.manualGearRotationOverride = true;
percentageDone = 0;
isRotatingToNewAngle = true;
}
}
}
xusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OutsideCornerGear : MonoBehaviour {
//0, 270, 180, 90
public enum possibleGravityOptions { UP, RIGHT, DOWN, LEFT };
public possibleGravityOptions[] possibleGravityCaresAbout;
public bool isRotatingToNewAngle = false;
public possibleGravityOptions currentGravity;
public possibleGravityOptions goalGravity;
public float rotationSpeed = 1f;
public float percentageDone = 0f;
PlayerMove playerScript;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (isRotatingToNewAngle) {
RotateAllThingsToNewState();
}
else {
if (playerScript != null) {
playerScript.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1;
}
}
}
//UP, Right, Down, Left
//0 , 1 , 2 , 3
//0, 270 , 180 , 90
//0, -90 , -180, -270
public void RotateAllThingsToNewState() {
int gearAmountToRotate = 270;
int gravityDifference = goalGravity - currentGravity;
if (gravityDifference > 0 && gravityDifference != 3 || gravityDifference == -3) {
gearAmountToRotate *= -1;
}
//Movement through gravity to the left rotates the gear clockwise
//Player rotates COUNTER clockwise
float startingGearRotation = 0;
//Rotation of the gear
this.transform.Rotate(new Vector3(0, 0, gearAmountToRotate * Time.deltaTime / rotationSpeed));
// this.transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, 0, startingGearRotation),
// Quaternion.Euler(0, 0, startingGearRotation + gearAmountToRotate), percentageDone);
//Rotation of the player around the gear
//float playerEndRotation = 90 - Vector2.Angle(this.transform.position, playerScript.gameObject.transform.position);
float playerEndRotation = 270;
if (gravityDifference> 0 && gravityDifference != 3 || gravityDifference == -3) {
playerEndRotation *= -1;
}
playerScript.gameObject.transform.RotateAround(gameObject.transform.position, Vector3.forward, playerEndRotation * Time.deltaTime / rotationSpeed);
//UP, Right, Down, Left
//0 , 1 , 2 , 3
//0, 270 , 180 , 90
//0, -90 , -180, -270
int playerAmountToRotate = 90;
if (goalGravity - currentGravity == -3 || goalGravity - currentGravity == 1) {
playerAmountToRotate *= -1;
}
float playerStartingRotation = 90 * ((4 - (int)currentGravity) % 4);
//Rotation of the player around itself
playerScript.gameObject.transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, 0, playerStartingRotation),
Quaternion.Euler(0, 0, playerStartingRotation + playerAmountToRotate), percentageDone);
percentageDone += Time.deltaTime / rotationSpeed;
if (percentageDone >= .9f) {
//Finish rotation and let player move again
isRotatingToNewAngle = false;
playerScript.manualGearRotationOverride = false;
//Lock rotation to the end state
this.transform.rotation = Quaternion.Euler(0, 0, startingGearRotation + gearAmountToRotate);
playerScript.gameObject.transform.rotation = Quaternion.Euler(0, 0, playerStartingRotation + playerAmountToRotate);
//Adjust the player's position so they don't immediately trigger the gear again
Vector3 playerFixPosition = new Vector3(0, 0, 0);
switch ((int)currentGravity) {
case 0: playerFixPosition = new Vector3(0, -.4f, 0); break;
case 1: playerFixPosition = new Vector3(-.4f, 0, 0); break;
case 2: playerFixPosition = new Vector3(0, .4f, 0); break;
case 3: playerFixPosition = new Vector3(.4f,0, 0); break;
default: Debug.Log("Things are horribly broken in fixing player location"); break;
}
playerScript.gameObject.transform.position += playerFixPosition;
//Cleanup of variables
this.gameObject.GetComponent<CircleCollider2D>().enabled = true;
playerScript.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1;
playerScript = null;
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player") {
playerScript = other.gameObject.GetComponent<PlayerMove>();
if(!playerScript.canRaycastUpLogic) {
playerScript = null;
return;
}
//Determine how the player is supposed to rotate as well as how gravity is supposed to move
if ((int)possibleGravityCaresAbout[0] == (int)playerScript.currentGravity) {
currentGravity = possibleGravityCaresAbout[0];
goalGravity = possibleGravityCaresAbout[1];
}
else if ((int)possibleGravityCaresAbout[1] == (int)playerScript.currentGravity) {
currentGravity = possibleGravityCaresAbout[1];
goalGravity = possibleGravityCaresAbout[0];
}
else {
playerScript = null;
return;
}
//Rotate Gravity to the New Direction
if (goalGravity - currentGravity == 3) {
playerScript.GravityRotate(-1);
}
else {
playerScript.GravityRotate(goalGravity - currentGravity);
}
//Allow Rotation to Begin
other.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
playerScript.manualGearRotationOverride = true;
percentageDone = 0;
isRotatingToNewAngle = true;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public enum GlobalGravityDirection { UP, RIGHT, DOWN, LEFT };
public GlobalGravityDirection currentGravity;
public LayerMask thingsCanHit;
public float secondsToSpin;
public float percentageSpun;
public float startingRotation;
public float goalRotation;
public float movementSpeed = 1;
public bool canRaycastUpLogic = true;
public bool canSwapGravity = true;
public bool manualGearRotationOverride = false;
public static bool playerIsMoving;
public Vector3 previousTransformPos;
public float marginOfError = 0.1f;
float colliderWidth;
float colliderHeight;
bool hasPoppedSound = false;
// Use this for initialization
void Start() {
previousTransformPos = transform.position;
currentGravity = GlobalGravityDirection.DOWN;
startingRotation = transform.eulerAngles.z;
colliderWidth = this.GetComponent<BoxCollider2D>().size.x;
colliderHeight = this.GetComponent<BoxCollider2D>().size.y;
}
void Update() {
if (manualGearRotationOverride) {
return;
}
if (RayCastDownInRange()) {
GravitySwap();
UpdateRotationSpeed();
this.GetComponent<Rigidbody2D>().gravityScale = 1;
if (Input.GetKey(KeyCode.Space) || !HasEnoughRoom()) {
this.GetComponent<Rigidbody2D>().gravityScale = 0;
this.GetComponent<Rigidbody2D>().velocity = -2 * Physics2D.gravity.normalized;
if (LeftSideRayCastDownInRange()) {
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
transform.Translate(movementSpeed * new Vector2(-1, 0), Space.Self);
}
}
if (RightSideRayCastDownInRange()) {
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
transform.Translate(movementSpeed * new Vector2(1, 0), Space.Self);
}
}
}
}
else {
if(!hasPoppedSound) {
hasPoppedSound = true;
this.GetComponent<AudioSource>().Play();
}
this.GetComponent<Rigidbody2D>().gravityScale = 1;
this.GetComponent<BoxCollider2D>().enabled = false;
canSwapGravity = true;
canRaycastUpLogic = false;
RotateToNewDirection();
}
if(Vector3.Distance(this.transform.position, previousTransformPos) > marginOfError || !canRaycastUpLogic) {
previousTransformPos = this.transform.position;
playerIsMoving = true;
CameraMove.toZoomCounter = 0.0f;
}
else {
previousTransformPos = this.transform.position;
playerIsMoving = false;
CameraMove.toZoomCounter+= Time.deltaTime;
}
}
//Lerps the player between starting rotation and the goal rotation determined by gravity
void RotateToNewDirection() {
//Determines rotations
if (startingRotation >= 360) {
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z % 360);
}
this.transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, 0, startingRotation), Quaternion.Euler(0, 0, goalRotation), percentageSpun);
if (percentageSpun < 1 && startingRotation != goalRotation) {
percentageSpun += Time.deltaTime / secondsToSpin;
}
if (percentageSpun >= .9) {
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, goalRotation % 360);
startingRotation = transform.eulerAngles.z;
canRaycastUpLogic = true;
this.GetComponent<BoxCollider2D>().enabled = true;
Debug.Log("EndRotation");
hasPoppedSound = false;
}
}
//This function reverses the direction of gravity
public void GravitySwap() {
if (!canSwapGravity) {
return;
}
canSwapGravity = false;
//Rotate around the enum
Vector2 gravityVector = new Vector2(0, 0);
currentGravity += 2;
if ((int)currentGravity > 3)
currentGravity -= 4;
//Actually Change Gravity
switch (currentGravity) {
case GlobalGravityDirection.UP: gravityVector = new Vector2(0, 9.8f); break;
case GlobalGravityDirection.RIGHT: gravityVector = new Vector2(9.8f, 0); break;
case GlobalGravityDirection.LEFT: gravityVector = new Vector2(-9.8f, 0); break;
case GlobalGravityDirection.DOWN: gravityVector = new Vector2(0, -9.8f); break;
default: Debug.Log("Error in Gravity Direction"); break;
}
Physics2D.gravity = gravityVector;
}
public void GravityRotate(int amountThePlayerRotates) {
//Rotate around the enum
Vector2 gravityVector = new Vector2(0, 0);
//Plus one if clockwise PLAYER rotation
currentGravity += amountThePlayerRotates;
if ((int)currentGravity > 3)
currentGravity -= 4;
if ((int)currentGravity < 0) {
currentGravity += 4;
}
//Actually Change Gravity
switch (currentGravity) {
case GlobalGravityDirection.UP: gravityVector = new Vector2(0, 9.8f); break;
case GlobalGravityDirection.RIGHT: gravityVector = new Vector2(9.8f, 0); break;
case GlobalGravityDirection.LEFT: gravityVector = new Vector2(-9.8f, 0); break;
case GlobalGravityDirection.DOWN: gravityVector = new Vector2(0, -9.8f); break;
default: Debug.Log("Error in Gravity Direction"); break;
}
Physics2D.gravity = gravityVector;
}
//Creates a raycast downward relative to the player
//Returns the Raycast2D created from that logicc
RaycastHit2D RayCastDown() {
float modifier = colliderWidth / 2;
Debug.DrawRay(this.transform.position, -1 * transform.up * colliderHeight, Color.green);
return Physics2D.Raycast(this.transform.position, -1 * transform.up, colliderHeight, thingsCanHit);
}
//A function to determine if the player character is in range of the environment
//Returns true if the play is in range
public bool RayCastDownInRange() {
if (!canRaycastUpLogic)
return false;
RaycastHit2D hit = RayCastDown();
return hit.collider != null;
}
public bool RightSideRayCastDownInRange() {
if (!canRaycastUpLogic)
return false;
float modifier = colliderWidth / 2;
RaycastHit2D hitRight = Physics2D.Raycast(this.transform.position + modifier * transform.right,
-1 * transform.up, colliderHeight, thingsCanHit);
Debug.DrawRay(this.transform.position + modifier * transform.right, -1 * transform.up * colliderHeight, Color.yellow);
if (hitRight.collider != null) {
return true;
}
return false;
}
public bool LeftSideRayCastDownInRange() {
if (!canRaycastUpLogic)
return false;
float modifier = colliderWidth / 2;
RaycastHit2D hitLeft = Physics2D.Raycast(this.transform.position - modifier * transform.right,
-1 * transform.up, colliderHeight, thingsCanHit);
Debug.DrawRay(this.transform.position - modifier * transform.right, -1 * transform.up * colliderHeight, Color.blue);
if (hitLeft.collider != null) {
return true;
}
return false;
}
public bool DoubleRayCastDownInRange() {
if (!canRaycastUpLogic)
return false;
float modifier = colliderWidth / 2;
RaycastHit2D hitLeft = Physics2D.Raycast(new Vector2(this.transform.position.x - modifier, this.transform.position.y),
-1 * transform.up, colliderHeight, thingsCanHit);
RaycastHit2D hitRight = Physics2D.Raycast(new Vector2(this.transform.position.x + modifier, this.transform.position.y),
-1 * transform.up, colliderHeight, thingsCanHit);
Debug.DrawRay(new Vector2(this.transform.position.x - modifier, this.transform.position.y), -1 * transform.up * colliderHeight, Color.green);
Debug.DrawRay(new Vector2(this.transform.position.x + modifier, this.transform.position.y), -1 * transform.up * colliderHeight, Color.green);
if (hitLeft.collider != null && hitRight.collider != null) {
return true;
}
return false;
}
float RayCastDownDistance() {
RaycastHit2D hit = RayCastDown();
if (hit.collider != null) {
return hit.distance;
}
return -1;
}
//This determines if the user is within range to latch onto a surface
void RayCastDownLogic() {
RaycastHit2D hit = RayCastDown();
if (hit.collider != null && canRaycastUpLogic) {
canRaycastUpLogic = false;
}
}
//Determines if the player character has enough space to rotate
public bool HasEnoughRoom() {
float modifier = colliderWidth / 2;
RaycastHit2D hitLeft = Physics2D.Raycast(this.transform.position - modifier * transform.right, transform.up, 100000000, thingsCanHit);
RaycastHit2D hitRight = Physics2D.Raycast(this.transform.position + modifier * transform.right, transform.up, 100000000, thingsCanHit);
if (hitLeft.collider != null && hitRight.collider != null) {
float leftDistanceToTravel = hitLeft.distance - (2f * colliderHeight);
float rightDistanceToTravel = hitRight.distance - (2f * colliderHeight);
if (leftDistanceToTravel > colliderHeight && rightDistanceToTravel > colliderHeight) {
if(leftDistanceToTravel - .1f < rightDistanceToTravel && leftDistanceToTravel + .1f > rightDistanceToTravel)
return true;
}
}
return false;
}
//This gives the user information on the distance to the next surface
//Additionally determines the speed of rotation of the user.
void UpdateRotationSpeed() {
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, transform.up, 100000000, thingsCanHit);
if (hit.collider != null) {
float distanceToTravel = hit.distance - (3.5f * colliderHeight);
percentageSpun = 0;
secondsToSpin = Mathf.Sqrt(2 * distanceToTravel / Physics2D.gravity.magnitude);
switch (currentGravity) {
case GlobalGravityDirection.UP: goalRotation = 180; startingRotation = 0; break;
case GlobalGravityDirection.RIGHT: goalRotation = 90; startingRotation = -90; break;
case GlobalGravityDirection.LEFT: goalRotation = 270; startingRotation = 90; break;
case GlobalGravityDirection.DOWN: goalRotation = 360; startingRotation = 180; break;
default: Debug.Log("Error in Rotation Logic"); break;
}
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, startingRotation);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.