Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 meowmeowmeow · May 08, 2014 at 02:49 AM · 3dmovement scriptjumping

My movement script is kind of broken for jumping (upward force is diminished after first jump)

This script I'm using on a Rigidbody object is KIND OF working. My first jump is pretty high, and all subsequent jumps are about 1/8th the force. What gives?

 #pragma strict
 
 var speed : float;
 var grounded : boolean = true;
 
 function Update () {
   var moveHorizontal : float = Input.GetAxis("Horizontal");
   var moveVertical : float = Input.GetAxis("Vertical");
   var moveUp : float;
   
   if (Input.GetKeyDown("space") && grounded == true) {
     moveUp = 20;
     grounded = false;
   }
   
   var movement : Vector3 = Vector3(moveHorizontal, moveUp, moveVertical);
   
   rigidbody.AddForce(movement * speed * Time.deltaTime);
 }
 
 function OnCollisionStay(collisionInfo : Collision) {
   grounded = true;
 }
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 Invertex · May 08, 2014 at 02:52 AM 0
Share

There are a few problems here and confusions.

  1. Functions affecting Forces/Rigidbodies, should be done in FixedUpdate(), not Update().

  2. You make "moveUp = 20" when space is pressed, but I don't see you ever setting it back to 0.

  3. Using OnCollisionStay will cause there to be a bit of a delay before the character is declared grounded. It's better to use OnCollisionEnter.

  4. OnCollisionStay will keep functioning on the frame after you hit jump, so it's going to set you back to grounded as soon as you jump.

What your Inspector while the game is playing, to see how your values are changing when you first try to jump and then after.

avatar image meowmeowmeow · May 08, 2014 at 02:56 AM 0
Share

So adding var moveUp : float = 0; and changing Update to FixedUpdate fixes the weird force thing. But now (and before--I failed to mention), I'm allowed a double jump. Grounded isn't unchecked until the second time I press space....

avatar image meowmeowmeow · May 08, 2014 at 03:13 AM 0
Share

RAD. Thank you. I'd like to mark your comments as an answer. How?

avatar image Invertex · May 08, 2014 at 03:16 AM 0
Share

No prob, and converted it! Thanks

1 Reply

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

Answer by Invertex · May 08, 2014 at 03:07 AM

Only physics functions should be done in FixedUpdate(), keypresses and other stuff should be done in in Update(). So when Space is pressed in Update(), it will change moveUp to 20, then when FixedUpdate() happens, it will operate based on those new values.

As for the double jump, did you switch to OnCollisionEnter?

Using Raycasts, or OverlapArea below your character, is a more consistent way to check if grounded though, simply disabling the check for a tiny bit after you hit jump. http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.OverlapArea.html

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Movement Code not working 2 Answers

Unity 3D: When walking off of object character falls almost instantly 2 Answers

Character moving in the wrong direction, glitched Rigidbody? 1 Answer

Movement Jumping Help 0 Answers

Collision with world object and object in Hand 0 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