Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created July 26, 2018 22:06
Show Gist options
  • Save Ratstail91/186dd9da309fac5e8183c165961408d8 to your computer and use it in GitHub Desktop.
Save Ratstail91/186dd9da309fac5e8183c165961408d8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.IO;
using UnityEngine;
[System.Serializable]
public class CharacterController2D : MonoBehaviour {
BoxCollider2D hitbox;
Rigidbody2D rigidBody;
public string statisticsFile;
public float spawnPointX;
public float spawnPointY;
//character-specific values, set in the character script
public float moveForce;
public float stopDivisor;
public float maxSpeed;
public float maxFallSpeed;
public float jumpForce;
public int airJumps;
public float scaleX;
public float scaleY;
//hitbox
public float hitboxOffsetX;
public float hitboxOffsetY;
public float hitboxSizeX;
public float hitboxSizeY;
//shortcut values
float hitboxLeft;
float hitboxRight;
float hitboxTop;
float hitboxBottom;
//movement
float horizontal = 0f;
bool jump = false;
bool grounded = false;
int airJumpCount = 0;
//graphics
float lastXSpeed = 0f;
void Awake() {
hitbox = GetComponent<BoxCollider2D> ();
rigidBody = GetComponent<Rigidbody2D> ();
}
IEnumerator Start() {
//hitbox
hitbox.offset = new Vector2 (hitboxOffsetX, hitboxOffsetY);
hitbox.size = new Vector2 (hitboxSizeX, hitboxSizeY);
//scale
Vector3 scale = transform.localScale;
scale.x *= scaleX;
scale.y *= scaleY;
transform.localScale = scale;
//calculate shortcuts
hitboxLeft = (hitboxOffsetX + hitboxSizeX/2) * scaleX;
hitboxRight = (hitboxOffsetX - hitboxSizeX/2) * scaleX;
hitboxTop = (hitboxOffsetY + hitboxSizeY/2) * scaleY;
hitboxBottom = (hitboxOffsetY - hitboxSizeY/2) * scaleY;
//BUG: can't jump before the first frame for some reason
yield return null;
Respawn ();
}
void Update() {
//determine if on the ground
grounded = Physics2D.Linecast(transform.position, transform.position + new Vector3( hitboxLeft, hitboxBottom - 0.01f, 0), 1 << LayerMask.NameToLayer("World"));
grounded |= Physics2D.Linecast(transform.position, transform.position + new Vector3(hitboxRight, hitboxBottom - 0.01f, 0), 1 << LayerMask.NameToLayer("World"));
//get horizontal speed, potentially negative
horizontal = Input.GetAxis ("Horizontal");
if (grounded) {
airJumpCount = 0;
}
//determine if jumping
if (Input.GetButtonDown ("Jump") && (grounded || airJumps > airJumpCount)) {
jump = true;
}
}
void FixedUpdate() {
//move in the inputted direction, if not at max speed
if (horizontal * rigidBody.velocity.x < maxSpeed) {
rigidBody.AddForce (Vector2.right * horizontal * moveForce);
}
//slow the player down horizontally if travelling too fast
if (Mathf.Abs (rigidBody.velocity.x) > maxSpeed) {
rigidBody.velocity = new Vector2 (Mathf.Sign (rigidBody.velocity.x) * maxSpeed, rigidBody.velocity.y);
}
//slow the player down if no input, or input is other direction
if (horizontal * rigidBody.velocity.x <= 0) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x/stopDivisor, rigidBody.velocity.y);
}
//slow the fall speed
if (rigidBody.velocity.y < maxFallSpeed) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x, Mathf.Sign (rigidBody.velocity.y) * -maxFallSpeed);
}
//jump up
if (jump) {
if (!grounded) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x, 0f);
airJumpCount++;
}
rigidBody.AddForce (new Vector2 (0f, jumpForce));
jump = false;
}
//flip the sprite to face the right way
if (rigidBody.velocity.x > 0.001f || rigidBody.velocity.x < -0.001f) {
lastXSpeed = rigidBody.velocity.x;
}
Vector3 scale = transform.localScale;
scale.x = lastXSpeed >= 0 ? Mathf.Abs(scale.x) : -Mathf.Abs(scale.x);
transform.localScale = scale;
}
void OnTriggerExit2D(Collider2D collider) {
Killbox killbox = collider.gameObject.GetComponent<Killbox> ();
if (killbox != null) {
Respawn ();
}
}
void Respawn() {
rigidBody.velocity = Vector2.zero;
rigidBody.position = new Vector2 (spawnPointX, spawnPointY);
}
void OnDrawGizmos() {
Gizmos.color = Color.red;
Gizmos.DrawLine (transform.position, transform.position + new Vector3 (hitboxLeft, hitboxBottom - 0.01f, 0));
Gizmos.DrawLine (transform.position, transform.position + new Vector3(hitboxRight, hitboxBottom - 0.01f, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment