- Home /
Input.GetButtonDown("Fire1") not working while 'moving'
Hi everyone,
I'm trying to destroy a cube by using a raycast to hit it every time i press the left mouse button. While standing perfectly still, this seems to work flawlessly. However, once my character is moving - even if just falling - the GetButtonDown command doesn't seem to register.
Code Below
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Debug.DrawRay(transform.position, transform.forward * 1000f, Color.green, 1000f);
shooting = true;
}
}
void FixedUpdate()
{
if (shooting)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 1000f))
{
if (hit.transform.tag == "Killable")
{
hit.collider.SendMessage("Hurt", damage, SendMessageOptions.DontRequireReceiver);
GameObject bloodcopy;
Blood.transform.position = hit.point;
bloodcopy = Instantiate(Blood, hit.point, Quaternion.identity);
GameObject.Destroy(bloodcopy, .3f);
}
}
shooting = false;
}
}
At first i thought it had something to do with the raycast itself while i was moving, I turned on drawing of the cast to help see what was happening, but couldn't see it. I then moved the draw to where it is above, and discovered that it would never draw while moving, but always draw when not moving.
What is going on?
Update: Using left CTRL key works perfectly, so the issue is only with mouse click.
You are shooting from your characters's forward. It he moves, the forward may not face that cube and hit the shot. Unity Input should always register.
The problem is that it isn't registering. Ignoring the cube, when i use the left mouse button while the character controller is in motion, the raycast is never drawn. By comparison, while moving and pressing left control key, the raycast is drawn. When the character controller isn't in motion (looking around is fine), the left mouse button does trigger and the ray is drawn.
Have you tried adding a Debug.Log statement to the if check in Update?
I would sprinkle a few Debug.Logs in there to deter$$anonymous$$e where the issue is. It's unlikely that it's with the Input call.
$$anonymous$$ore likely, the issue is with the raycast hitting the source collider.
Answer by Cuttlas-U · Apr 11, 2017 at 08:54 AM
hi; Try using " if (Input.GetButtonDown(0))"
see if that helps;
I can't think any thing out of this , seem u did it all correct;
Your answer
Follow this Question
Related Questions
Lerp in Coroutine (Crazy Behavior) 2 Answers
How long would it take to make a 2d game? 0 Answers
Problem importing "survival shooter" 0 Answers
shotgun like action 1 Answer
Rotation of wheels of a car 1 Answer