leomovskii

PlayerMove

Sep 26th, 2025
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlayerMove : MonoBehaviour {
  4.  
  5.     public float speed = 6;
  6.     public float jumpPower = 3;
  7.     public bool hasJumpsLimit = false;
  8.     public int jumpsLimit = 2;
  9.  
  10.     public Rigidbody2D controller;
  11.     public Transform cam;
  12.  
  13.     private float x;
  14.     private int jumpsDone;
  15.  
  16.     void Update() {
  17.         x = Input.GetAxis("Horizontal");
  18.  
  19.         if (x > 0) {
  20.             transform.rotation = Quaternion.Euler(0, 0, 0); // поворот направо
  21.         }
  22.  
  23.         if (x < 0) {
  24.             transform.rotation = Quaternion.Euler(0, 180, 0); // поворот наліво
  25.         }
  26.  
  27.         if (Input.GetButtonDown("Jump")) {
  28.             if (hasJumpsLimit && jumpsDone >= jumpsLimit) {
  29.                 return;
  30.             }
  31.  
  32.             controller.velocity = new Vector2(controller.velocity.x, jumpPower);
  33.             jumpsDone += 1;
  34.         }
  35.     }
  36.  
  37.     void FixedUpdate() {
  38.         controller.velocity = new Vector2(x * speed, controller.velocity.y); // рух по горизонталі
  39.         cam.position = new Vector3(transform.position.x, transform.position.y, -10);
  40.     }
  41.  
  42.     void OnCollisionEnter2D(Collision2D c) {
  43.         if (c.transform.CompareTag("Ground")) {
  44.             jumpsDone = 0;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment