Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created March 19, 2019 08:36
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 Ratstail91/3f3c95131bf73f3a157862233b92c24a to your computer and use it in GitHub Desktop.
Save Ratstail91/3f3c95131bf73f3a157862233b92c24a to your computer and use it in GitHub Desktop.
The Player Controller script stripped to it's core.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCharacterController : MonoBehaviour {
//internal components
Rigidbody2D rigidBody;
BoxCollider2D boxCollider;
//constants
const float deadZone = 0.25f; //axis input deadzone
const float groundedProjection = 0.08f; //how far into the ground to project the linecasts
//gameplay (DOCS: These settings are calibrated for 1 pixel = 1 unity unit)
[Header("Movement Settings")]
public float gravityScale = 100;
public float moveForce = 1000f;
public float jumpForce = 30000;
public float maxSpeed = 300f;
public float fallSpeed = 800f;
//basic movement
float horizontalInput = 0f;
float verticalInput = 0f;
bool jumping = false;
bool grounded = false;
void Awake() {
rigidBody = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
}
void Start() {
//
}
void Update() {
HandleInput();
}
void FixedUpdate() {
//debug
rigidBody.gravityScale = gravityScale;
HandleMovement();
}
void HandleInput() {
//determine if on the ground (using coyote time)
bool trueGrounded;
trueGrounded = Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
trueGrounded |= Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x + boxCollider.size.x / 2.1f), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
trueGrounded |= Physics2D.Linecast(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x - boxCollider.size.x / 2.1f), -groundedProjection, 0), 1 << LayerMask.NameToLayer("Ground"));
if (trueGrounded) {
grounded = true;
} else {
StartCoroutine(SetGroundedAfter(false, 0.1f)); //coyote physics: 100ms
}
//get inputs
verticalInput = GamePad.GetAxis(CAxis.LY);
horizontalInput = GamePad.GetAxis(CAxis.LX);
//flip direction
if (Mathf.Abs(horizontalInput) >= deadZone) {
transform.localScale = new Vector3(horizontalInput > 0 ? 1 : -1, 1, 1);
}
//determine if jumping
if (GamePad.GetState().Pressed(CButton.A) && grounded) {
jumping = true;
}
}
void HandleMovement() {
//stop the player if input in that direction has been removed
if (horizontalInput * rigidBody.velocity.x <= 0 && grounded) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x * 0.85f, rigidBody.velocity.y);
}
//move in the inputted direction, if not at max speed
if (horizontalInput * rigidBody.velocity.x < maxSpeed) {
rigidBody.AddForce (Vector2.right * horizontalInput * moveForce);
}
//slow the player down when it's travelling too fast
if (Mathf.Abs(rigidBody.velocity.x) > maxSpeed) {
rigidBody.velocity = new Vector2 (Mathf.Sign(rigidBody.velocity.x) * maxSpeed, rigidBody.velocity.y);
}
if (rigidBody.velocity.y < -fallSpeed) {
rigidBody.velocity = new Vector2 (rigidBody.velocity.x, Mathf.Sign(rigidBody.velocity.y) * fallSpeed);
}
//jump up
if (jumping) {
rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0f); //max v-jump speed
rigidBody.AddForce (new Vector2 (0f, jumpForce));
jumping = false;
}
}
void OnDrawGizmos() {
if (boxCollider != null) {
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x), -groundedProjection, 0));
Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x + boxCollider.size.x / 2.1f), -groundedProjection, 0));
Gizmos.DrawLine(transform.position + new Vector3(boxCollider.offset.x, boxCollider.offset.y, 0), transform.position + new Vector3(transform.localScale.x * (boxCollider.offset.x - boxCollider.size.x / 2.1f), -groundedProjection, 0));
}
}
IEnumerator SetGroundedAfter(bool value, float delay) {
yield return new WaitForSeconds(delay);
grounded = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment