- Home /
(2D) character jumping problem [is grounded detection, rigidbody2d]
Hello. There you can see my script. I try to make a 2D Jump and Run game. But sometimes if i hold the space button (for jumping) my character makes a unwanted "mega jump". I think because my character is grounded too long and the script adds the force to the 2d rigidbody two or more times. Can someone help me ?
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
private Rigidbody2D rb;
public float JumpForce = 1000;
public bool isGroundet = false;
public bool Jump = false;
public Transform [] groundCheck;
public LayerMask whatIsGround;
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
}
void FixedUpdate () {
rb.velocity = new Vector2(Speed, rb.velocity.y);
if (Input.touchCount > 0 || Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
Jump = true;
}
isGroundet = false;
if (Physics2D.OverlapCircle(groundCheck[0].position, .15f, whatIsGround))
{
isGroundet = true;
}
if (Jump && isGroundet&&rb.velocity.y<JumpForce)
{
rb.AddForce(Vector2.up * JumpForce);
Jump = false;
}
}
}
Answer by Masterio · Jun 07, 2016 at 07:30 PM
Try to reset a valocity before:
rb.velocity = new Vector2(rb.velocity.x, 0f);
and then add the force:
rb.AddForce(Vector2.up * JumpForce);
Or simplify all of it by:
rb.velocity = new Vector2(rb.velocity.x, JumpForce);
Answer by Eno-Khaon · Jun 07, 2016 at 10:34 PM
For best results, it's handy to keep in mind that inputs are registered during Update(). This means that OnGUI(), with double the framerate of Update(), will register inputs twice per frame and FixedUpdate(), not matching the timing of Update(), may occasionally miss inputs.
With that in mind, my example will break input and usage into two sections:
// Updated touch input to be based on when the touch is first applied
// Similarly, updated keyboard input to a GetKeyDown check
void Update()
{
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
Jump = true;
}
}
void FixedUpdate()
{
// Multiplied velocity check by 0.95 for safety
if (Jump && isGroundet && rb.velocity.y < JumpForce * 0.95f)
{
// Force applied is immediate.
rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
Jump = false;
}
}
To note, you would also want to divide your current "JumpForce" by about 50 (default physics step) in order to bring the jumping velocity back down to where you'd want it.
Edit: Whoops, right. Rigidbody2D.
Your answer
Follow this Question
Related Questions
Coroutine not working properly 1 Answer
Rotate towards velocity (2D) 2 Answers
2d grounded check problem? 3 Answers
2D Geometry dash-like ship physics 0 Answers
How can I move a 2D GameObject left/right relative to its forward direction? 1 Answer