Created
October 19, 2017 21:04
-
-
Save chaseWilliams/b45bc8b6cf5b5e698e1b6709aff4f0be to your computer and use it in GitHub Desktop.
Old School Platformer Controls in Unity
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 PlayerControllerOldSchool : MonoBehaviour | |
{ | |
public float jumpForce = 800f; | |
public float moveSpeed = 5f; | |
bool isGrounded = false; | |
float airtime = 0; | |
float GRAVITY = -9.81f; | |
Rigidbody2D rb; | |
void Awake() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
} | |
void Update() | |
{ | |
float h_input = Input.GetAxisRaw("Horizontal"); | |
float v_input = Input.GetKeyDown(KeyCode.UpArrow) ? 1 : 0; | |
// set horizontal velocity | |
if (Mathf.Abs(h_input) > Mathf.Epsilon) { | |
float x_velocity = Mathf.Sign(h_input) * moveSpeed; | |
rb.velocity = new Vector2(x_velocity, rb.velocity.y); | |
} | |
// jump | |
if (v_input == 1 && isGrounded) | |
{ | |
rb.AddForce(new Vector2(0, jumpForce)); | |
isGrounded = false; | |
} | |
} | |
private void OnCollisionEnter2D(Collision2D collision) | |
{ | |
isGrounded = true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment