2d Mobile game physics gravity not working

Hello,

I am making a simple EASY and yes trying to make it easy 2d mobile android game for fun/test, but i got the run right and left working with button, but jumping, doesnt work, i looked at all tutorials online, youtube etc., they point to the same way or similiar way of jumping:

rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpForce);

HOWEVER, my character never jumps, i even checked the gravity, i put debug.log() to test if the button is pressed, jump, YUP that works, however the character never jumps, i used brackeys, and other tutorials to help me get this working, but NOTHING!!! i am at my wits end with unity… i am willing to share my entire project to help assist on finding out what am i doing wrong :frowning: any help, suggestions etc… i will copy and paste the code, but i believe if i share my project, it will bring more light to the problem, please give me email if interested so i can email you the entire project, its about 79 mb zipped file.

below is the CharacterControls.cs i have attached to the character object to jump, i have rigidbody2d already set and collider, plus the GameObject that is invisible set as well to see if it has touched the ground. and yes i set that layer as well.

please… again i am at my wits end and wondering if unity is even good to make mobile games or should i try coco or godot for mobile games?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class CharacterControl : MonoBehaviour
{

    float dirX;
    public float moveSpeed = 10f;
   
    Rigidbody2D rb;
    bool facingRight = true;
    Vector3 localScale;
    public Animator animator;

    private bool isGrounded = false;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;

    private bool jump = false;

    public float jumpForce;

    // Use this for initialization
    void Start ()
    {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D> ();
    }
   
    // Update is called once per frame
    void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis ("Horizontal");

        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
       
        if(isGrounded && jump == true)
        {
            //rb.velocity = Vector2.up * jumpForce;
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpForce);
        }
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2 (dirX * moveSpeed, 0);
        if (rb.velocity.magnitude > 0)
        {
            animator.SetBool("Run", true);
        }
        else
            animator.SetBool("Run", false);
    }

    void LateUpdate()
    {       
        CheckWhereToFace ();
    }

    public void JumpCheck(bool checkjump)
    {
        if (checkjump == true)
        {
            jump = true;
        }
    }

    void CheckWhereToFace ()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;

        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
        {
            localScale.x *= -1;
        }
       
        transform.localScale = localScale;
    }

}

Unity wouldn’t be good for anything if it couldn’t make a character jump. :wink:

In the FixedUpdate you’re setting the y-axis velocity to zero so it shouldn’t be surprising that when you add a y-axis velocity in Update, it’s reset.

Here’s the minor modification. Note that continually reading properties adds up, it’s best to grab some value once then modify that i.e. “var velocity = rb.velocity;” etc. Also, you might want to not continously set the Transform scale, even to the same thing if you can help it. Try making changes only when things change such as direction etc.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class CharacterControl : MonoBehaviour
{
    float dirX;
    public float moveSpeed = 10f;
 
    Rigidbody2D rb;
    bool facingRight = true;
    Vector3 localScale;
    public Animator animator;
    private bool isGrounded = false;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool jump = false;
    public float jumpForce;
    // Use this for initialization
    void Start ()
    {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D> ();
    }
 
    // Update is called once per frame
    void Update()
    {
        dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
  
        if(isGrounded && jump == true)
        {
            //rb.velocity = Vector2.up * jumpForce;
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * jumpForce);
        }
    }
    void FixedUpdate()
    {
        // Ensure we only change the x-axis velocity!!
        rb.velocity = new Vector2 (dirX * moveSpeed, rb.velocity.y);
        if (rb.velocity.magnitude > 0)
        {
            animator.SetBool("Run", true);
        }
        else
            animator.SetBool("Run", false);
    }
    void LateUpdate()
    { 
        CheckWhereToFace ();
    }
    public void JumpCheck(bool checkjump)
    {
        if (checkjump == true)
        {
            jump = true;
        }
    }
    void CheckWhereToFace ()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;
        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
        {
            localScale.x *= -1;
        }
  
        transform.localScale = localScale;
    }
}

Hey Melv, thanks for helping, still trying, i literally copied and paste your code, and still didnt work, i made sure the Kinematic is disabled and its using Dynamic, but still nothing, what is worse if i use urs, the running part left or right is acting weird, so i changed it back and, i am sadly back to square one :frowning: any other thoughts or input would only help.

thanks again

There are some things that the code doesn’t show such as is the grounded working. You should try using “Debug.Log()” to see what the state is at certain points such as checking the body velocity, seeing if the player is flagged as grounded when it should be etc.