Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerMove : MonoBehaviour {
- public float moveSpeed = 6f;
- public float jumpPower = 6f;
- Rigidbody2D rb;
- bool onGround;
- private void Start() {
- rb = GetComponent<Rigidbody2D>();
- }
- private void Update() {
- float input = Input.GetAxis("Horizontal"); // -1 .. 0 .. 1
- if (Input.GetButtonDown("Jump") && onGround) {
- rb.velocity = new Vector2(moveSpeed * input, jumpPower);
- onGround = false;
- } else {
- rb.velocity = new Vector2(moveSpeed * input, rb.velocity.y);
- }
- }
- private void OnCollisionEnter2D(Collision2D collision) {
- onGround = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement