Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug 2D player not jumping

Discussion in 'Physics' started by Liam_GameDev, Sep 2, 2023.

  1. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    I am having a problem with getting my player to jump. The input is working the ground check is working but the player still won't jump
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    79
    Give us something to work with. we don't know how you're trying to jump, anything about the code, etc.

    This is also not classified as a bug but instead user error.
     
    MelvMay likes this.
  3. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    here's the code

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;

    public class playerMovment : MonoBehaviour
    {
    public int playerSpeed;
    public int playerHealth;
    public int jumpPower;
    public Rigidbody2D rb;
    private float horizontalMovement;
    private bool isFacingRight;
    public LayerMask ground;
    public Transform groundCheck;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    rb.velocity = new Vector2 (horizontalMovement * playerSpeed, rb.velocity.y);

    if (!isFacingRight && horizontalMovement < 0f)
    {
    flip();
    }
    else if (isFacingRight && horizontalMovement > 0f)
    {
    flip();
    }
    }

    public void jump(InputAction.CallbackContext context)
    {
    if (context.performed && isGrounded())
    {
    rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpPower);
    Debug.Log("jump");
    }
    else if (context.canceled && rb.velocity.y > 0f)
    {
    rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    }
    }

    private bool isGrounded()
    {
    return Physics2D.OverlapCircle(groundCheck.position,0.2f,ground);
    }

    private void flip()
    {
    isFacingRight = !isFacingRight;
    Vector2 localscale = transform.localScale;
    localscale.x *= -1f;
    transform.localScale = localscale;
    }

    public void Move(InputAction.CallbackContext context)
    {
    horizontalMovement = context.ReadValue<Vector2>().x;
    }
    }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,776
    As you already know, please don't post code as plain text. Always use code-tags.

    You need to debug your code. You cannot simply post it and hope others can debug it for you. You've sprinkled it with debug.logs but don't state what's output. Also, you're not debugging what you're using as arguments i.e. what forces, velocities etc. You can attached a debugger and do this or just output them. Knowing you called a method is not enough.

    This is not likely to be a physics issue but a script issue.

    Thanks.
     
    POOKSHANK likes this.
  5. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    79
    i'll give you a hint, when you multiply a number the result changes drastically based on the second value. zero for example