- Home /
Input.GetButtonDown inconsistent
I'm having trouble getting the Input.GetButtonDown to react consistently with this script. It works, but often times it will ignore mouse clicks. I can't figure out why it is acting with such inconsistency. Here is the code I'm working off:
// *Trip* Sidescroller Script // Based off the Unity FPS Walker //
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; private var moveDirection = Vector3.zero; private var grounded : boolean = false; var theCamera : Camera; var playerObj : GameObject; var playerCoordinates; var relative;
// Create variables for click jump var mousePosition; var AllowClickJump : boolean = true; var clickJump : float = 2; var JumpSideForce : float; var JumpUpForce : float; var StraightJump : float; var airJumps; var allowedAirJumps : int = 2;
function Awake() { clickJump = clickJump/10; }
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Mouse Jump
if (Input.GetButtonDown("Fire1") && AllowClickJump && airJumps < allowedAirJumps)
{
airJumps++;
mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
mousePosition = theCamera.ScreenToViewportPoint(mousePosition);
playerCoordinates = new Vector3(playerObj.transform.localPosition.x, playerObj.transform.localPosition.y,
playerObj.transform.localPosition.z);
pCoords = theCamera.ScreenToViewportPoint(playerCoordinates);
Debug.Log("Mouse Click: " + mousePosition + "Char Pos" + pCoords);
// Determine if the mouse click was left or right of the player
if(mousePosition.x < .4)
{
mousePosition.x = -JumpSideForce;
mousePosition.y = JumpUpForce;
}
if(mousePosition.x > .6)
{
mousePosition.x = JumpSideForce;
mousePosition.y = JumpUpForce;
}
if(mousePosition.x >.4 && mousePosition.x <.6)
{
mousePosition.y = StraightJump;
}
// Translate to moving action
moveDirection = transform.TransformDirection(mousePosition);
moveDirection = new Vector3(moveDirection.x, moveDirection.y, 0);
// Apply the jump + modifier
moveDirection *= clickJump;
} //End mouse jump
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if(grounded == true)
{
airJumps = 0;
}
}
@script RequireComponent(CharacterController)
Answer by Mike 3 · Jun 26, 2010 at 08:59 PM
You shouldn't use GetButtonDown with FixedUpdate
FixedUpdate isn't called every frame, and sometimes multiple times a frame, which means it'll screw up the GetButtonDown return
You should use Update instead for the button handling (and then process it in the next FixedUpdate if it's to do with physics)
awarded 10 points. I was having this issue randomly, so I printed the inputs happening, and shockingly they were not all firing despite the variables they'd have to go through. Put my button inputs on an update function, and now nothing is missing its input!
I would've never deduced it was fixed updates fault, so thank you very much for this insight.
Your answer
Follow this Question
Related Questions
Jumping not allways registering fix? 0 Answers
counter doesnt work in update 2 Answers
[C#] Make my 2D character jump in FixedUpdate 0 Answers
Problem with jump script 1 Answer
Jumping randomly doesn't work 2 Answers