Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerMove : MonoBehaviour {
- public float speed = 6;
- public float jumpPower = 3;
- public bool hasJumpsLimit = false;
- public int jumpsLimit = 2;
- public Rigidbody2D controller;
- public Transform cam;
- private float x;
- private int jumpsDone;
- void Update() {
- x = Input.GetAxis("Horizontal");
- if (x > 0) {
- transform.rotation = Quaternion.Euler(0, 0, 0); // поворот направо
- }
- if (x < 0) {
- transform.rotation = Quaternion.Euler(0, 180, 0); // поворот наліво
- }
- if (Input.GetButtonDown("Jump")) {
- if (hasJumpsLimit && jumpsDone >= jumpsLimit) {
- return;
- }
- controller.velocity = new Vector2(controller.velocity.x, jumpPower);
- jumpsDone += 1;
- }
- }
- void FixedUpdate() {
- controller.velocity = new Vector2(x * speed, controller.velocity.y); // рух по горизонталі
- cam.position = new Vector3(transform.position.x, transform.position.y, -10);
- }
- void OnCollisionEnter2D(Collision2D c) {
- if (c.transform.CompareTag("Ground")) {
- jumpsDone = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment