Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ElektronikTV · Jan 09, 2018 at 02:18 PM · 2d2d-platformer2d-physics

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);
     }
 
 }
 


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Jan 09, 2018 at 03:29 PM 0
Share

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.

avatar image ElektronikTV · Jan 09, 2018 at 04:32 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Jan 09, 2018 at 04:08 PM 0
Share

I can guarantee that Unity's gravity does indeed work. What is your "gravity" value set to in your inspector?

avatar image ElektronikTV · Jan 09, 2018 at 04:14 PM 0
Share

Its set to 40. The character with the 3D rigidbody falls faster than the character with the 2D Rigidbody.

avatar image TreyH ElektronikTV · Jan 09, 2018 at 04:16 PM 0
Share

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.

avatar image ElektronikTV · Jan 09, 2018 at 04:18 PM 0
Share

1 for both characters.

avatar image TreyH ElektronikTV · Jan 09, 2018 at 04:21 PM 0
Share

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?

avatar image ElektronikTV · Jan 09, 2018 at 04:33 PM 0
Share

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

avatar image TreyH ElektronikTV · Jan 09, 2018 at 04:50 PM 0
Share

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.

avatar image TreyH ElektronikTV · Jan 09, 2018 at 04:54 PM 0
Share

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");
     }
 }

avatar image ElektronikTV · Jan 09, 2018 at 04:55 PM 1
Share

Thanks for helping :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis). 1 Answer

Softbody physics with 2D sprites 1 Answer

Jumping from special jump orb 1 Answer

Problem with Falling Platform Script 3 Answers

Unity 2D player sticks on platform corners 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges