• 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 Salmando · Apr 11, 2017 at 03:39 AM · unity 5getbuttondown

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?

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 Salmando · Apr 10, 2017 at 10:26 AM 0
Share

Update: Using left CTRL key works perfectly, so the issue is only with mouse click.

avatar image estevan95 · Apr 11, 2017 at 02:22 PM 0
Share

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.

avatar image Salmando estevan95 · Apr 11, 2017 at 02:29 PM 0
Share

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.

avatar image FortisVenaliter · Apr 11, 2017 at 03:59 PM 0
Share

Have you tried adding a Debug.Log statement to the if check in Update?

I would sprinkle a few Debug.Logs in there to determine 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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

move target gameobject on collision with projectile 1 Answer

Apple App review problem: IAP not working on Ipad 0 Answers

Unity 5 specular realtime GI not working in build 2 Answers

How to monitor the movment speed of a 2D sprite 0 Answers

UnassignedReferenceException and Invalid Cast exception 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges