Forum begins after the advertisement:


[Part 1] Better way of coding jump

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] Better way of coding jump

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #17647
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Reposting @l1nked712‘s suggestion:

    In my opinion a simpler way to implement jumping is:
    [Header("Jump")]
    [SerializeField] private float jumpForce = 20;
    private bool isGrounded = true;
    
    void Start()
    {
          ...
    }
    
    void Update()
    {
           ...
           Jump();
    }
    
    private void OnCollisionEnter2D(Collision2D collision)
    {
         isGrounded = true;
    }
    
    private void Jump()
    {
         if(Input.GetButtonDown("Jump") && isGrounded)
         {
                 rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
                 isGrounded = false;
          }
    }

    Of course you could add extra constraints to the jump functions if such as if its actually colliding with the ground by adding a tag and then using gameObject.CompareTag but just for the square on this simple floor this would in my opinion be a much simpler implementation. Feel free to correct me if im wrong or if something is suboptimal in this solution.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: