Rodeo Armada is a team project developed for a Drexel class. As part of the project, I maje the player controller, UI, & obstacle collisions.
Last active
October 26, 2023 16:06
-
-
Save polygonalcube/bc84aebb5759b067178025fdcda2f55e to your computer and use it in GitHub Desktop.
Rodeo Armada (team) | Player, UI, & obstacle spawning code I wrote. https://ommiller.itch.io/rodeo-armada
This file contains hidden or 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 GameManager : MonoBehaviour | |
| { | |
| public float scroll_speed; | |
| public int prev_enemies_killed; | |
| public int enemies_killed; | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (enemies_killed > prev_enemies_killed) | |
| { | |
| scroll_speed -= 1f; | |
| enemies_killed = prev_enemies_killed; | |
| } | |
| } | |
| } |
This file contains hidden or 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 Obstacle : MonoBehaviour | |
| { | |
| private Rigidbody2D rb; | |
| private GameManager gm; | |
| public int maxDistance; | |
| void Start() | |
| { | |
| rb = GetComponent<Rigidbody2D>(); | |
| GameObject preGm = GameObject.Find("Game Manager"); | |
| gm = preGm.GetComponent<GameManager>(); | |
| } | |
| void FixedUpdate() | |
| { | |
| rb.velocity = new Vector2(gm.scroll_speed, 0); | |
| if (transform.position.x < maxDistance) | |
| { | |
| Destroy(this.gameObject); | |
| } | |
| } | |
| void OnTriggerEnter2d(Collider2D collision) | |
| { | |
| if (collision.tag == "Border") | |
| Destroy(this.gameObject); | |
| } | |
| } |
This file contains hidden or 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 PlayerControl : MonoBehaviour | |
| { | |
| /* | |
| Components needed: | |
| - Rigidbody 2D (set to Dynamic, Gravity Scale set to 0) | |
| - Collider 2D (ideally Capsule Collider 2D) | |
| - Transform, child of Player object | |
| Features include: | |
| - Adjustable jump height (tile-based) | |
| - Fastfalling (pressing down makes you fall faster) | |
| - Variable jump height (let go of jump while still going upwards and you will stop ascending) | |
| */ | |
| //Variables, with ideal values and explanations when needed. | |
| //Various gravity variables. The idea behind this is that the best platformers have you ascend slowly and descend faster, which is what the first two variables are for. | |
| //When you press down while in midair, you'll fall even faster than descending gravity. | |
| //These variables affect speed_y. | |
| public float gravity_rising; //-19.62 | |
| public float gravity_falling; //-39.24 | |
| public float gravity_fastfall; //-98.1 | |
| private float speed_y; | |
| public float jumpHeight; //3, number represents height in tiles. | |
| private bool isGrounded; | |
| private bool isJumping; | |
| private Rigidbody2D rb; | |
| public SpriteRenderer sprite; | |
| //Coyote time if you prefer. hangTime is the amount of time in seconds, after the player has run off of an edge, that the player has to jump. | |
| public float hangTime; //0.2f | |
| private float hangCounter; | |
| //An input buffer but for jumping. jumpBufferLength is the amount of time in seconds before the player hits the ground, that the player can press jump and the input will be accepted. | |
| public float jumpBufferLength; //0.1f | |
| private float jumpBufferCounter; | |
| //Necessary for the custom ground checking method. Unity's ground check is a bit finicky. | |
| public Transform groundCheck; | |
| public float groundDistance = 0.5f; | |
| public LayerMask groundMask; | |
| public int health; | |
| public float iFrames; //seconds; amount of time that the player stays invincible after getting hit | |
| void Start() | |
| { | |
| isGrounded = false; | |
| isJumping = false; | |
| iFrames = 0; | |
| rb = GetComponent<Rigidbody2D>(); | |
| //sprite = GetComponent<SpriteRenderer>(); | |
| } | |
| void Update() | |
| { | |
| isGrounded = Physics2D.CircleCast(new Vector2(groundCheck.position.x, groundCheck.position.y), groundDistance, new Vector2(0f,0f), Mathf.Infinity, groundMask); | |
| if (speed_y < 0) | |
| { | |
| isJumping = false; | |
| } | |
| //gravity switching | |
| if (Input.GetAxisRaw("Vertical") == -1) | |
| { | |
| speed_y += gravity_fastfall * Time.deltaTime; | |
| } | |
| else if (isJumping) | |
| { | |
| speed_y += gravity_rising * Time.deltaTime; | |
| } | |
| else | |
| { | |
| speed_y += gravity_falling * Time.deltaTime; | |
| } | |
| //coyote time | |
| if(isGrounded) | |
| { | |
| //speed_y = -2f; This line makes jumping inconsistent for some reason. | |
| hangCounter = hangTime; | |
| } | |
| else | |
| { | |
| hangCounter -= Time.deltaTime; | |
| } | |
| //jump buffer | |
| if(Input.GetButtonDown("Jump")) | |
| { | |
| jumpBufferCounter = jumpBufferLength; | |
| } | |
| else | |
| { | |
| jumpBufferCounter -= Time.deltaTime; | |
| } | |
| //allows for jumping | |
| if(jumpBufferCounter > 0f && hangCounter > 0f) | |
| { | |
| //handy formula for setting exact jump height | |
| speed_y = Mathf.Sqrt(jumpHeight * -2f * gravity_rising); | |
| jumpBufferCounter = 0; | |
| isJumping = true; | |
| } | |
| if (Input.GetButtonUp("Jump") && speed_y > 4f) | |
| { | |
| speed_y = 4f; | |
| } | |
| rb.velocity = new Vector2(0, speed_y); | |
| if (iFrames > 0) | |
| { | |
| sprite.enabled = !sprite.enabled; | |
| iFrames -= Time.deltaTime; | |
| } | |
| else | |
| { | |
| sprite.enabled = true; | |
| } | |
| if (health <= 0) | |
| { | |
| Destroy(gameObject); | |
| } | |
| } | |
| /*void OnTriggerEnter2D(Collider2D col) | |
| { | |
| if (col.tag == "Obstacle") | |
| { | |
| Destroy(this.gameObject); | |
| } | |
| }*/ | |
| //Visualizes the ground check radius in the editor. | |
| void OnDrawGizmosSelected() | |
| { | |
| Gizmos.DrawWireSphere(groundCheck.position, groundDistance); | |
| } | |
| } |
This file contains hidden or 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 PlayerHitDetection : MonoBehaviour | |
| { | |
| public PlayerControl player; | |
| public float maxIFrames; | |
| void OnTriggerEnter2D(Collider2D col) | |
| { | |
| if (player.iFrames <= 0) | |
| { | |
| if (col.tag == "Obstacle") | |
| { | |
| player.health--; | |
| player.iFrames = maxIFrames; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 SpawnObstacles : MonoBehaviour | |
| { | |
| public GameObject obstacle; | |
| public float maxX; | |
| public float minX; | |
| public float maxY; | |
| public float minY; | |
| public float timeBetweenSpawn; | |
| private float spawnTime; | |
| public GameManager gm; | |
| void Start() | |
| { | |
| GameObject game = GameObject.Find("Game Manager"); | |
| gm = game.GetComponent<GameManager>(); | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (Time.time > spawnTime) | |
| { | |
| Spawn(); | |
| spawnTime = Time.time + timeBetweenSpawn; | |
| } | |
| } | |
| void Spawn() | |
| { | |
| float randomX = Random.Range(minX, maxX); | |
| float randomY = Random.Range(minY, maxY); | |
| Instantiate(obstacle, transform.position + new Vector3(randomX, randomY, 0), transform.rotation); | |
| timeBetweenSpawn -= .05f; | |
| if (timeBetweenSpawn < 1f) | |
| { | |
| timeBetweenSpawn = 1f; | |
| } | |
| } | |
| } |
This file contains hidden or 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; | |
| using UnityEngine.UI; | |
| using UnityEngine.SceneManagement; | |
| public class UI : MonoBehaviour | |
| { | |
| public Button retryButton; | |
| public PlayerControl player; | |
| // Start is called before the first frame update | |
| void Start() | |
| { | |
| retryButton.gameObject.SetActive(false); | |
| } | |
| // Update is called once per frame | |
| void Update() | |
| { | |
| if (player.health <= 0) | |
| { | |
| retryButton.gameObject.SetActive(true); | |
| } | |
| } | |
| public void Retry() | |
| { | |
| SceneManager.LoadScene("Game"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment