Created
April 20, 2020 18:51
-
-
Save saturngamesss/86f83779ae9d68fbe48482d067197235 to your computer and use it in GitHub Desktop.
Basic 2D movement code for Unity
This file contains 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
//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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NICE...thank you so much