Skip to content

Instantly share code, notes, and snippets.

@saturngamesss
Created April 20, 2020 18:51
Show Gist options
  • Save saturngamesss/86f83779ae9d68fbe48482d067197235 to your computer and use it in GitHub Desktop.
Save saturngamesss/86f83779ae9d68fbe48482d067197235 to your computer and use it in GitHub Desktop.
Basic 2D movement code for Unity
//FROM PROJECT ROROPE || REAL GAMES STUDIO
//************************************************
//realgamesss.weebly.com
//gamejolt.com/@Real_Game
//realgamesss.newgrounds.com/
//real-games.itch.io/
//youtube.com/channel/UC_Adg-mo-IPg6uLacuQCZCQ
//************************************************
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D rb;
public float speed = 50;
public float jumpForce = 20;
public bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetKey(KeyCode.D))
{
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else if (Input.GetKey(KeyCode.A))
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
if (Input.GetKey(KeyCode.Space) && isGrounded == true)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}
}
@nefandev
Copy link

nefandev commented Jul 7, 2021

NICE...thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment