• 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 PersianKiller · Jul 24, 2017 at 12:17 PM · updaterigidbody2dvector2fixedupdategetbuttondown

My character won't stop jumping!!!

vector2 JUMP; public float JumpHeight;

 void Update () {

jumping();

}

public void jumping(){

     if (Input.GetButtonDown ("Jump") && GetComponent<Rigidbody2D>().velocity.y==0  ) {

         JUMP=new Vector2 (GetComponent<Rigidbody2D>().velocity.x,JumpHeight);

     }
 
 }


void FixedUpdate(){

         GetComponent<Rigidbody2D> ().velocity = JUMP;

     }


Can anyone tell me how can I fix it? I want to jump only once before I hit the ground, but when I push button jump it goes to air and won't stop jumping.

,

Comment
Add comment · Show 4
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 ownerfate · Jul 24, 2017 at 12:24 PM 0
Share

does your character just bonce a lot, or does it go up and not come back down?

avatar image PersianKiller ownerfate · Jul 24, 2017 at 12:29 PM 0
Share

it goes up and not come back down :)

avatar image ownerfate PersianKiller · Jul 24, 2017 at 01:10 PM 0
Share

okay, here is what's going on.

if you press the space bar, while the rigidbody's velocity is 0, it adds to the JumpHeight value to the Y axis causing it to go up, and not come back down.

if you did something like:

if( GetComponent().velocity.y>JumpHeight){ velocity.y==0; }

i don't work with 2Drigidbodies, but this may help,

if it ends up going through the floor.... well what went up, came back down.... at least...

Show more comments

1 Reply

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

Answer by SeaLeviathan · Jul 24, 2017 at 01:35 PM

The problem is that the first time you tell it to jump, you just set the velocity to something.

What you are probably looking for is the AddForce function.

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

Try looking at the example to get a better idea of how this component works!

 {
     
     public int JumpHeight;
     public Rigidbody2D rb;
     void Update()
     {
         jumping();
 
     }
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>(); // or you can just manually set this in the inspector, as it is a public variable.
     }
 
     public void jumping()
     {
 
         if (Input.GetButtonDown("Jump"))
         {
             rb.AddForce((Vector2.up * JumpHeight));
         }
 
     }
     void FixedUpdate()
     {
         jumping();
         
     }
 }
 

This will make it so that every time you press spacebar it jumps - what you were trying to do with the velocity.y = 0 would let you be able to spam the spacebar and jump endlessly, every time the jump apexes. Try instead a character controller, it has a built in grounded function. If you want to go full custom, however, you can use either raycasting or triggers or collision detections. The choice is really yours, if you want more help on any aspect of your character jumping just tell me!

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 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

Related Questions

Rigidbody2d.MovePosition on Update vs FixedUpdate 1 Answer

Jumping randomly doesn't work 2 Answers

Checkibg Rigidbody2d and component problems 0 Answers

Can the Script Execution order be adjusted for FixedUpdate()? 3 Answers

Character won't move (Fixed) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges