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
3
Question by imalousyhero · Jul 07, 2012 at 12:57 AM · c#2dcontrollerjumpplatformer

GetButtonDown not always firing

I have a basic character movement script for a 2d platformer. My problem is that when using "GetButtonDown" the "jump" action only executes about 75% of the time. The rest of the time it completely ignores the input. If I change that to "GetKey(KeyCode.Space)" it works perfect every time, but, I get the bunny-hop effect. Any help is greatly appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
  
  #region Variables
  
  public float speed = 6.0f;
  public float jumpSpeed = 8.0f;
  public float gravity = 9.81f;
  private bool canJump;
  private Vector3 moveDirection = Vector3.zero;
  
  #endregion
  
  
  #region Functions
  
  void Update()
  {
  CharacterController controller = GetComponent<CharacterController>();
  
  if (controller.isGrounded)
  {
  moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
  moveDirection = transform.TransformDirection(moveDirection);
  moveDirection *= speed;
  
  if (!Input.GetKey(KeyCode.Space))
  {
  canJump = true;
  }
  
  if (Input.GetKey(KeyCode.Space) && canJump == true)
  {
  moveDirection.y = jumpSpeed;
  canJump = false;
  }
  }
  controller.Move(moveDirection * Time.deltaTime);
  moveDirection.y -= gravity * Time.deltaTime;
  
  Debug.Log(controller.isGrounded ? "GROUNDED" : "NOT GROUNDED");
  }
  
  #endregion
 }



UPDATE:

I added the debug log to detect if the controller was grounded or not and I noticed that it was flipping back and forth from "grounded" to "not grounded" when I wasn't even moving or jumping. This looks like the reason that my jump isn't reliable... any idea why this is happening?

Comment
Add comment · Show 6
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 Linus · Jul 07, 2012 at 04:10 AM 0
Share

Try GetButtonUp and keep an eye on the value of isGrounded while running the game

avatar image fafase · Jul 07, 2012 at 07:26 AM 0
Share

GetButtonUp is the exact same principle as GetButtonDown. Only does it return true when released ins$$anonymous$$d of pressed.

avatar image Linus · Jul 07, 2012 at 07:58 AM 0
Share

Ok, during a Walker Boys tutorial there was some issues by using GetButtonDown and fixedUpdate. Thought it was worth testing, your answer should solve it though

avatar image Wolfram · Jul 09, 2012 at 02:30 PM 0
Share

I added the debug log to detect if the controller was grounded or not and I noticed that it was flipping back and forth from "grounded" to "not grounded" when I wasn't even moving or jumping. This looks like the reason that my jump isn't reliable... any idea why this is happening?

No, it is impossible to tell, since you did not post any code that actually modifies controller.isGrounded, the problem must be there.

Also, please do not edit and just completely change your code example as a reaction to other answers and comments. Now the code is totally unrelated to the title of your question, and the answers no longer make sense, since they no longer refer to your current code. This makes it impossible for others to benefit from the actual question and the answers.

Ins$$anonymous$$d, you should keep the original code, and maybe post any changes (if they become necessary) below that, in a clearly marked section (such as EDIT, or UPDATE, as you did with your text).

avatar image imalousyhero · Jul 09, 2012 at 02:55 PM 0
Share

I moved the code from FixedUpdate to Update and added the Debug at the bottom of the code... I don't see how this changes any potential answer to the actual problem. I apologize for not doing that and will keep it in $$anonymous$$d for further questions.

Correct me if I'm wrong, but, isn't isGrounded just a wrapper for collision flag detection and is modified by the character controller itself colliding with anything below it. $$anonymous$$eaning that I don't actually modify it with anything? Regardless, this is the only script in my scene and is applied to a cube(with character controller) that is sitting on top of a rectangle with a box collider.

Show more comments

1 Reply

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

Answer by SarperS · Jul 07, 2012 at 07:19 AM

Instead of using FixedUpdate, move your code to Update method. FixedUpdate gets called based on even intervals, the times it ignores your input is the times that it's not updated/called between those intervals.

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 imalousyhero · Jul 09, 2012 at 12:27 AM 0
Share

Ah, I read that anything involving movement or physics should be put in FixedUpdate ins$$anonymous$$d of Update. If this was the case how come my movement is fine and only the jump is getting screwed up? I'll try this when I get back to Unity and post my results.

avatar image Linus · Jul 09, 2012 at 10:41 AM 0
Share

physics should go in FixedUpdate, USer input information in Update

avatar image imalousyhero · Jul 09, 2012 at 01:34 PM 0
Share

I have changed the code to use Update() ins$$anonymous$$d of FixedUpdate() and updated my example above. I get the same problem but I think I've narrowed it down to the isGrounded function. It keeps flipping back and forth from being grounded to not grounded.

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

9 People are following this question.

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

Related Questions

Player stucken in the wall while jumping 2D 1 Answer

I'm making a 2D mobile platformer and I just figured out how to make the mobile buttons work. But I can't get the jump limit to work. Does anyone know how to create maybe a ground detection with this script? 1 Answer

Character doesn't jump repeatedly 1 Answer

How can I change jump direction? 1 Answer

How to fix Jump problem with 2d Platformer Unity demo game? 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