형들 골드메탈영상보고 배운거 토대 + 구글링통해서 복습겸 마리오 구현하기 하고있는데

유니티 관련 사람들 예기하는거 들어보니까


input 관련  -  update() 에 구현

physics 관련(rigidbody, addforce, linearvelocity등) -  fixedupdate() 에 구현


한다고 하더라고


그래서 원래 점프관련된거를 전부 update에 구현했다가

다음과 같이 바꿨는데 이게 점프가 안되는데 왜그럴까?


마리오해본사람은 알겠지만 점프 꾹누르면 더 높이 올라가게 코딩했음


public class Mario : MonoBehaviour

{

    Rigidbody2D rb;


    public float maxSpeed;

    

    float horizontal;

    float facingDirection = 1;


    bool isGrounded = true;


    public float jumpForce = 12f;

    public float holdForce = 4f;

    public float maxHoldTime = 0.2f;

   

    private bool isJumping;

    private float jumpHoldTimer;

  

    private bool smallJump;

    private bool bigJump;

    private bool stopJump;


    private void Awake()

    {

        rb = GetComponent<Rigidbody2D>();

    }


    private void Update() {


        // MOVEMENT INPUT

        horizontal = Input.GetAxisRaw("Horizontal");

        


        if (Input.GetButtonDown("Jump") && isGrounded)

        {

            smallJump = true;

            Debug.Log(smallJump);

        }

        if (Input.GetButton("Jump") && isJumping)

        {

            bigJump = true;

            Debug.Log(bigJump);

        }

        if (Input.GetButtonUp("Jump"))

        {

            stopJump = true;

            Debug.Log(stopJump);

        }

    }


    private void FixedUpdate()

    {

        // Movement

        rb.AddForce(Vector2.right * horizontal, ForceMode2D.Impulse);


        if (rb.linearVelocity.x > maxSpeed)

        {

            rb.linearVelocity = new Vector2(maxSpeed, rb.linearVelocity.y);

        }

        if (rb.linearVelocity.x < maxSpeed * -1)

        {

            rb.linearVelocity = new Vector2(maxSpeed * (-1), rb.linearVelocity.y);

        }


        // Jump

        Jump();


        // Flip

        if (horizontal >= .1f && facingDirection < 0 || horizontal <= -.1f && facingDirection > 0)

        {

            Flip();

        }

    }


    private void OnCollisionEnter2D(Collision2D collision)

    {

        if (collision.gameObject.tag == "Material")

        {

            isGrounded = true;

        }

    }


    private void Jump()

    {

        if (smallJump)

        {

            rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);

            isJumping = true;

            isGrounded = false;

            jumpHoldTimer = maxHoldTime;

            smallJump = false;

        }

        if (bigJump && isJumping)

        {

            if (jumpHoldTimer > 0f)

            {

                rb.AddForce(Vector2.up * holdForce, ForceMode2D.Force);

                jumpHoldTimer -= Time.fixedDeltaTime;

            }

            else

            {

                isJumping = false;

            }

        }

        if (stopJump)

        {

            isJumping = false;

            stopJump = false;

        }

        bigJump = false;

    }

...