• 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 WaifuSlayer · Jul 13, 2017 at 11:47 AM · scripting problemjumpingbug-perhaps

Strange behavior of my jump script

Hi, i'm new to unity and trying to write a simple player movement script on my own, with a jump set on space bar that has the basic rules of a video game jump (no jump mid-air, jump during a movement is taken in account...) Here is what i came up with : In FixedUpdate :

     if (Input.GetKeyDown("space") && transform.position.y < 0.5f )
         {
             Jump();
             
         } 


          void Jump(){
     rigidBody.AddForce (Vector3.up * jumpHeight);
 }

Jump is outside of FixedUpdate of course. My problem is that sometimes, the player object will only move by a few units and sometimes it'll make a huge jump. So far i couldn't find any pattern to understand the logic between the different jumps.

Is there a way to code a more reliable jump, i want to set a height and have my player object do a nice and smooth jump of that height. and if the player is moving during the jump, i want the object to jump in that direction.

Thanks for your help :)

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 Vollmondum · Jul 13, 2017 at 12:40 PM 1
Share

You need to remove your y position < 0.5f, unless you're planning to move on flat surface all the time, not having any slopes that go over 0.5.You'd better replace it with Rigidbody.velocity.y

avatar image WaifuSlayer Vollmondum · Jul 13, 2017 at 01:24 PM 0
Share

I changed that part for a isFalling boolean linked with an OnCollisionStay function, thank you.

But the problem persists : The jumps are at random heights no matter what i do, and it is not controlled by the amount of time i press space consecutively or the time spent pressing...

avatar image Justice-V18 WaifuSlayer · Jul 13, 2017 at 02:47 PM 0
Share

Try Changing:

 rigidBody.AddForce (Vector3.up * jumpHeight);

To:

 rigidBody.AddForce (Vector3.up * jumpHeight, Force$$anonymous$$ode.Impulse);

avatar image Jwizard93 · Jul 13, 2017 at 05:06 PM 0
Share

FixedUpdate() is not suitable for input because it doesn't run every frame. Use Update here ins$$anonymous$$d. And testing for y is bad for isGrounded style checking.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by abhishek7 · Jul 13, 2017 at 02:43 PM

Ok, so as for the grounding mechanism use OnCollisionEnter() and OnCollisionExit() to change a boolean for whether the object is grounded or not, and remove the transform.position.y<0.5 . To do this just place a tag on the ground object and change the boolean whenever it collides with the tagged ground.

Try using

 rigidBody.velocity = new Vector3(rigidBody.velocity.x, jumpHeight, rigidBody.velocity.z);
 

That should hopefully resolve your problem.

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
avatar image
1

Answer by springwater · Jul 13, 2017 at 02:44 PM

you should probably use forcemode.impulse or velocity change. https://docs.unity3d.com/ScriptReference/ForceMode.html

Comment
Add comment · Show 3 · 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 WaifuSlayer · Jul 13, 2017 at 06:42 PM 0
Share

Thanks, but i already tried that and although it makes the jumps a bit more stable, i still have two distinct patterns that are randoms it seems...

avatar image Jwizard93 WaifuSlayer · Jul 13, 2017 at 06:57 PM 1
Share

The only explanation for the strange behavior (assuming there isn't other code interacting that you haven't posted) is due to the fact that you are getting input in a function that doesn't run every frame. It's hard to say when and how many times addforce will actually be called when you receive Input in FixedUpdate()

avatar image WaifuSlayer Jwizard93 · Jul 13, 2017 at 07:47 PM 0
Share

So, is there any way of fixing this? Apart from the jump function i only have planar movement which are designed pretty much like those in the survival shooter tutorial.

avatar image
0

Answer by Mercbaker · Jul 13, 2017 at 08:08 PM

You can probably just avoid all these problems by adding a "Character Controller", Component on your Player object

Then just add the code from this official Unity script example onto your Player object.

You can edit the fine details on the Component, and the Player movement in the above script example.

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

113 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

Related Questions

I use this code to jump, but it's just teleporting my player upwards. It works totally perfect in an other project. 3 Answers

I'm trying to allow my character to only jump when grounded 1 Answer

Multi-object editing not supported on ANY scripts in entire project. 1 Answer

Calculating jump while rotating around pivot 1 Answer

Character triple+ Jumping: Please Help! 0 Answers

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