- Home /
C# 2D Jumping with AddForce doesnt work
I tried to make a Jump sript for a 2D platformer but it didnt work. So I made a Jump script for a 3D box and it work fine. Than i changed all the 3D parts (Rigidbody, Collider) to 2D and used a 2D character but he didnt jump. The force is applied but the object didnt move. I also made the smae character with 3D Collider and 3D Rigidbody and it worked. Are there major differences between the 2D and 3D Rigidbody?
The script for the 2D Jump:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public float jumpforce;
public float speed;
public float gravity;
float relativeGravity;
bool isGrounded = false;
Rigidbody2D rbody;
// Use this for initialization
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
private void OnTriggerStay2D(Collider2D collision)
{
isGrounded = true;
Debug.Log("isGrounded: " + isGrounded);
}
private void FixedUpdate()
{
//Gravity
if (rbody.velocity.y < 0)
{
relativeGravity = gravity * 2;
}
else
{
relativeGravity = gravity;
}
rbody.AddForce(Vector2.down * relativeGravity);
//Jump
if (Input.GetButton("Jump") && isGrounded)
{
rbody.AddForce(new Vector2(0, 1) * jumpforce,ForceMode2D.Impulse);
isGrounded = false;
Debug.Log("Jumped");
}
//Movement
rbody.MovePosition(new Vector2(rbody.position.x + Input.GetAxis("Horizontal") * speed, rbody.position.y));
Debug.Log("velocity.y: " + rbody.velocity.y);
}
}
Does it work without checking isGrounded? This is a common question for this forum, but sometimes people don't always check that each piece works fine by itself.
I have some problems with upload the image but its just the default Rigidbody2D with gravity scale set to 0. When I deactivate the Jump script it works normal. When I just use my gravity(no jump) in the script its falling. When I just use the Jump mechanic it does do nothing.
Answer by ElektronikTV · Jan 09, 2018 at 04:05 PM
No. Each time I press Jump the velocity rises but dont fall again. The object falls down until it touches the ground. The velocity.y is 0 until I hit Jump. Than it rises. I also tried build in Gravity.
I can guarantee that Unity's gravity does indeed work. What is your "gravity" value set to in your inspector?
Its set to 40. The character with the 3D rigidbody falls faster than the character with the 2D Rigidbody.
what is the mass of your object? Force is the product of two values, mass and acceleration. If your object is barely moving, it might be related to its mass.
can you post a picture of your rigidbody component? does the object fall normally if you drop it in scene-view when the game is running?
I have some problems with upload the image but its just the default Rigidbody2D with gravity scale set to 0. When I deactivate the Jump script it works normal. When I just use my gravity(no jump) in the script its falling. When I just use the Jump mechanic it does do nothing
Pasting your code into my editor, it looks like your lines:
//$$anonymous$$ovement
rbody.$$anonymous$$ovePosition (new Vector2 (rbody.position.x + Input.GetAxis ("Horizontal") * speed, rbody.position.y));
Debug.Log ("velocity.y: " + rbody.velocity.y);
Are the issue.
What happens when you replace your FixedUpdate() with this? You can work out the isGrounded on your own I think:
// $$anonymous$$ey inputs usually work best in frame-specific Update
bool shouldJump = false;
void Update ()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
this.shouldJump = true;
}
}
private void FixedUpdate ()
{
//Gravity
if (rbody.velocity.y < 0) {
relativeGravity = gravity * 2;
} else {
relativeGravity = gravity;
}
rbody.AddForce (Vector2.down * relativeGravity + Vector2.right * Input.GetAxis ("Horizontal") * speed);
if (this.shouldJump) {
this.rbody.AddForce (new Vector2 (0, 1) * this.jumpforce, Force$$anonymous$$ode2D.Impulse);
this.shouldJump = false;
Debug.Log ("JU$$anonymous$$PING");
}
}
Your answer
Follow this Question
Related Questions
Softbody physics with 2D sprites 1 Answer
Jumping from special jump orb 1 Answer
Problem with Falling Platform Script 3 Answers