miepzerino
2023-12-08 f39be74450278982fe7d96484fae083cc4d7dcbf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.ComponentModel.Design;
using UnityEngine;
using UnityEngine.InputSystem;
 
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float maxFallSpeed = -20f;
    Vector2 moveInput;
 
    public bool IsMoving { get; private set; }
 
    Rigidbody2D rb;
 
    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
 
    // Start is called before the first frame update
    void Start()
    {
 
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
 
    private void FixedUpdate()
    {
 
        Debug.Log(moveInput.x);
        if (moveInput.y == 0)
        {
            if (rb.velocity.y <= maxFallSpeed)
            {
                // max fall speed, dont accelerate more
                rb.velocity = new Vector2(moveInput.x * moveSpeed, maxFallSpeed);
            }
            else
            {
                // normal fall
                rb.velocity = new Vector2(moveInput.x * moveSpeed, rb.velocity.y);
            }
        }
        else
        {
            if (rb.velocity.y < 0 && moveInput.y > 0)
            {
                // falling but moving upwards
                rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed) + rb.velocity.y);
            }
            else
            {
                // moving upwards no falling
                rb.velocity = new Vector2(moveInput.x * moveSpeed, (moveInput.y * moveSpeed));
            }
        }
    }
 
    public void OnMove(InputAction.CallbackContext context)
    {
        moveInput = context.ReadValue<Vector2>();
 
        IsMoving = moveInput != Vector2.zero;
 
    }
}