Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class PlayerMove2D : MonoBehaviour {
- public float moveSpeed = 5f;
- public float jumpPower = 5f;
- public int jumpsLimit = 2;
- Rigidbody2D _rigidbody;
- int _jumpsDone;
- float _horizontalInput;
- bool _wantToJump;
- private void Start() {
- _rigidbody = GetComponent<Rigidbody2D>();
- }
- private void Update() {
- _horizontalInput = Input.GetAxis("Horizontal") * moveSpeed; // -1 .. 1
- if (Input.GetButtonDown("Jump")) {
- _wantToJump = true;
- }
- if (_horizontalInput > 0) {
- transform.localEulerAngles = new Vector3(0f, 0f, 0f);
- } else if (_horizontalInput < 0) {
- transform.localEulerAngles = new Vector3(0f, 180f, 0f);
- }
- }
- private void FixedUpdate() { // 0.02
- if (_wantToJump && _jumpsDone < jumpsLimit) { // true/false
- _rigidbody.velocity = new Vector2(_horizontalInput, jumpPower);
- _jumpsDone++;
- } else {
- _rigidbody.velocity = new Vector2(_horizontalInput, _rigidbody.velocity.y);
- }
- _wantToJump = false;
- }
- private void OnCollisionEnter2D(Collision2D collision) {
- _jumpsDone = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement