Skip to content

Instantly share code, notes, and snippets.

@e-sarkis
Last active March 17, 2018 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-sarkis/329704cb139248f587ebecf390945e52 to your computer and use it in GitHub Desktop.
Save e-sarkis/329704cb139248f587ebecf390945e52 to your computer and use it in GitHub Desktop.
The PlayerController component from my game, BulletFish!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[HideInInspector] public GameController gc; // reference to the scene's gamemanager object's gamecontroller component
[HideInInspector] public ParticleSystem Bubbles; // reference to the scene's gamemanager object's gamecontroller component
public GameObject playerGun; // reference to Player's gun
public float moveForce = 11f;
public float jumpForce = 750f;
public float jumpSpeed = 0.5f;
public float minJumpTime = 1.0f;
private float jumpTimerCountdown;
private Rigidbody2D rb;
private Collider2D c;
private SpriteRenderer sr;
private HealthPoints hp;
private Animator anim;
private float inputX = 0f;
private float inputFall = 0f;
[HideInInspector] public bool facingLeft = false;
public bool grounded = true;
private bool alive = true;
[HideInInspector] public bool takingDamage = false;
public Transform foot;
public float rayDist = 1.0f;
void Awake ()
{
gc = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
rb = GetComponent<Rigidbody2D>();
c = GetComponent<Collider2D>();
sr = GetComponent<SpriteRenderer>();
hp = GetComponent<HealthPoints>();
anim = GetComponent<Animator>();
Bubbles = GetComponentInChildren<ParticleSystem>();
jumpTimerCountdown = 0f;
}
void Start()
{
gc.UpdateHPDisplay(hp.HP);
}
void Update ()
{
if (hp.isZero)
{
alive = false;
sr.color = Color.black;
anim.enabled = false;
Bubbles.Stop();
playerGun.GetComponent<GunController>().enabled = false;
} else if (takingDamage)
{
takingDamage = false;
}
if (!grounded)
{
anim.SetBool("isFalling", true);
} else
{
anim.SetBool("isFalling", false);
}
// Cast Ray to see if we're grounded
Debug.DrawLine(transform.position, new Vector2(transform.position.x, transform.position.y) + (rayDist * Vector2.down), Color.red);
if (Physics2D.Raycast(transform.position, Vector2.down, rayDist).collider.gameObject.tag == "Floor")
{
grounded = true;
Bubbles.Stop();
}
if (transform.position.y > gc.StageTop.transform.position.y)
{
transform.position = new Vector3(transform.position.x, gc.StageTop.transform.position.y, transform.position.z);
rb.velocity = new Vector2(rb.velocity.x, 0);
}
if (alive)
{
Move();
} else
{
gc.GameOver();
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Floor" && (coll.gameObject.transform.position.y <= foot.position.y || rb.velocity.y < 0))
{
grounded = true; // we're touching the floor
Bubbles.Stop();
}
if (coll.gameObject.tag == "Enemy" && !coll.gameObject.GetComponent<HealthPoints>().isZero)
{
EnemyController ec = coll.gameObject.GetComponent<EnemyController>();
hp.SendMessage("ApplyDamage", ec.damageOnContact);
takingDamage = true;
coll.gameObject.GetComponent<ColliderManager>().DisableColliderTemporarily();
AudioController.instance.PlaySound("Impact1");
}
if (coll.gameObject.tag == "Bomb")
{
Bomb bomb = coll.gameObject.GetComponent<Bomb>();
hp.SendMessage("ApplyDamage", bomb.damageOnContact);
takingDamage = true;
bomb.Explode();
}
}
void OnCollisionStay2D(Collision2D coll)
{
if (coll.gameObject.tag == "Floor" && (coll.gameObject.transform.position.y <= foot.position.y))
{
grounded = true; // we're touching the floor
Bubbles.Stop();
}
}
void OnCollisionExit2D(Collision2D coll)
{
if (coll.gameObject.tag == "Floor" )
{
grounded = false; // we're no longer touching the floor
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "PickupPoints" && !hp.isZero)
{
PickupPoints Pts = coll.gameObject.GetComponent<PickupPoints>();
gc.addPoints(Pts.value);
Destroy(coll.gameObject);
AudioController.instance.PlaySound("Pickup0");
}
}
private void Move()
{
Debug.Log("Y Velocity: " + rb.velocity.y);
// obtain movement input
inputX = Input.GetAxisRaw("Horizontal");
// Update Animator State
if (Mathf.Abs(inputX) > 0)
{
anim.SetBool("hasXInput", true);
} else
{
anim.SetBool("hasXInput", false);
}
inputFall = -(Mathf.Abs(Input.GetAxisRaw("Vertical")));
// Apply X and Y movement if valid input exists
float XForce = moveForce * inputX;
rb.velocity = new Vector2(XForce, rb.velocity.y);
if (grounded && Input.GetButtonDown("Jump")) // Correct situation for a new jump?
{
grounded = false;
AudioController.instance.PlaySound("Jump0"); // Play JUMP sound
if (Bubbles.isStopped) Bubbles.Play(); // Start Bubbles
jumpTimerCountdown = minJumpTime;
}
if (jumpTimerCountdown > 0) // Engaged in stage one jump
{
rb.velocity = new Vector2(XForce, jumpSpeed);
jumpTimerCountdown -= Time.deltaTime;
if (jumpTimerCountdown <= 0) // End stage one jump
{
rb.velocity = new Vector2(XForce, 0);
rb.AddForce(new Vector2(0, jumpForce)); // Little boost to make mock gravity effect
}
}
if (!grounded) if (Bubbles.isStopped) Bubbles.Play();
// correct Sprite based on direction of movement
if (XForce < 0 && !facingLeft && Time.timeScale > 0)
{
Flip();
facingLeft = true;
}
else if (XForce > 0 && facingLeft && Time.timeScale > 0)
{
Flip();
facingLeft = false;
}
}
// Flip SpriteRenderer in the X axis.
private void Flip()
{
if (sr.flipX) { sr.flipX = false; } else { sr.flipX = true; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment