Need some help with implementing jumping.

Ive gone through the Ruby 2d tutorial and wanted to try something on my own so I used some of the script from the tutorial and another script from a website but I cant get it to work.

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

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rigidbody2d;
   
    float horizontal;
    float vertical;
    public float speed = 3.0f;

    public float jumpForce;

    bool isGrounded = false;
    public Transform isGroundedChecker;
    public float checkGroundRadius;
    public LayerMask groundLayer;

    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;

    public float rememberGroundFor;
    float lastTimeGrounded;

    public int defaultAdditionalJumps = 1;
    int additionalJumps;

    Animator animator;
    Vector2 lookDirection = new Vector2(1, 0);

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxis("Horizontal");
        vertical = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);

        if (!Mathf.Approximately(move.x, 0.0f) )
        {
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
        }

        animator.SetFloat("Look X", lookDirection.x);
       
        animator.SetFloat("Speed", move.magnitude);

        Jump();
        CheckIfGrounded();
        BetterJump();

    }
    private void FixedUpdate()
    {
        Vector2 position = rigidbody2d.position;

        position.x = position.x + speed * horizontal * Time.deltaTime;
        position.y = position.y + speed * vertical * Time.deltaTime;

        rigidbody2d.MovePosition(position);
    }
    void Jump()
    {
        if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundFor || additionalJumps > 0))
        {
            rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, jumpForce);
            additionalJumps--;
        }
    }
    void CheckIfGrounded()
    {
        Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
        if (collider != null)
        {
            isGrounded = true;
            additionalJumps = defaultAdditionalJumps;
        }
        else
        {
            if (isGrounded)
            {
                lastTimeGrounded = Time.time;
            }

            isGrounded = false;
        }
    }
    void BetterJump()
    {
        if (rigidbody2d.velocity.y < 0)
            rigidbody2d.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        else if (rigidbody2d.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
            rigidbody2d.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;

    }
}

It’s long ago where I had to worry about jumping I now use my old scripts to jump but I think it work like that at best:

void Update()
{
if(Input. GetKeyDown(KeyCode.Space) && isGrounded)
{
  rb2d.AddForce(transform.up * jumpMultiplier, ForceMode.Impulse);
}

void OnCollisionEnter2D(Collision2D other)
{
  if(other.gameObject.tag == "Ground")
  {
    isGrounded = true;
   }
 }

 void OnCollisionExit2D() 
 {
  isGrounded = false;
 }
}

Thats how I jump in my games. Very simple

1 Like