Advertisement
leomovskii

PlayerMove

Sep 21st, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerMove : MonoBehaviour {
  4.  
  5.     public float moveSpeed = 6f;
  6.     public float jumpPower = 6f;
  7.    
  8.     Rigidbody2D rb;
  9.     bool onGround;
  10.  
  11.     private void Start() {
  12.         rb = GetComponent<Rigidbody2D>();
  13.     }
  14.  
  15.     private void Update() {
  16.         float input = Input.GetAxis("Horizontal"); // -1 .. 0 .. 1
  17.  
  18.         if (Input.GetButtonDown("Jump") && onGround) {
  19.             rb.velocity = new Vector2(moveSpeed * input, jumpPower);
  20.             onGround = false;
  21.         } else {
  22.             rb.velocity = new Vector2(moveSpeed * input, rb.velocity.y);
  23.         }
  24.     }
  25.  
  26.     private void OnCollisionEnter2D(Collision2D collision) {
  27.         onGround = true;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement