- Home /
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?
Try GetButtonUp and keep an eye on the value of isGrounded while running the game
GetButtonUp is the exact same principle as GetButtonDown. Only does it return true when released ins$$anonymous$$d of pressed.
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
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).
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.
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.
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.
physics should go in FixedUpdate, USer input information in Update
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.