Question by
loony220792 · Jun 29, 2018 at 11:21 AM ·
jumpaddforcemarioholding
Mario jump addforce problem
Hi, I need help with jumping like a mario. I found this solution on the Internet, but I don't like the physics of the jump because of the velocity. When changing to addforce, only a short jump works, as it is not respected (rb.velocity.y> jumpMinForce). I can not understand what condition to ask and what to change, so that everything works with Addforce. Thank you
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BetterJump_2 : MonoBehaviour {
public float jumpMinForce = 3f;
public float jumpForce = 10f;
bool jump = false;
bool jumpCancel = false;
public LayerMask whatIsGround;
private bool isGrounded = false;
public Transform groundCheck;
private float groundRadius = 0.2f;
private Rigidbody2D rb;
void Awake (){
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
// Check ground
if (Input.GetButtonDown("Jump") && isGrounded) // Start Press Jump
jump = true;
if (Input.GetButtonUp("Jump") && !isGrounded) // Stop Press Jump
jumpCancel = true;
}
void FixedUpdate()
{
// Simple Jump
if (jump)
{
rb.AddForce(Vector2.up*jumpForce, ForceMode2D.Impulse);
// rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jump = false;
}
// Cancel Jump
if (jumpCancel)
{
if (rb.velocity.y > jumpMinForce)
// rb.velocity = new Vector2(rb.velocity.x, jumpMinForce);
rb.AddForce(Vector2.up*jumpMinForce, ForceMode2D.Impulse);
jumpCancel = false;
}
}
}
Comment