Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 30, 2020 at 08:20 AM by pako for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by tmalhassan · Oct 31, 2017 at 10:00 AM · c#unity 5mousevelocityhold

Input.GetMouseButton(0) registering twice on relase?

I am using Input.GetMouseButton(0) to add force to my character. The problem is that it seems to register twice when I release the mouse button. Why is this happening? this is the code I am using:

 if (Input.GetMouseButton(0))
 {
     controller.velocity = Vector2.zero;
     controller.AddForce(new Vector2(0, 75f));
 }

I am not using any mouse or touch controls anywhere else in my game.

Any help is very much appreciated.

Comment
Add comment
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

4 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by pako · Oct 31, 2017 at 10:27 AM

  if (Input.GetMouseButton(0))
  {
        if(!GetMouseButtonUp(0)){
         controller.velocity = Vector2.zero;
         controller.AddForce(new Vector2(0, 75f));
        }
 
  }

Comment
Add comment · Show 10 · 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 tmalhassan · Oct 31, 2017 at 10:35 AM 0
Share
 Input.Get$$anonymous$$ouseButtonDown(0)

will return true once. I want to add the force as long as the mouse button is held. $$anonymous$$y problem is that it adds the force twice once I release the mouse button. So it looks like its adding 150f to the velocity.

avatar image AidanHorton tmalhassan · Oct 31, 2017 at 11:11 AM 0
Share

I'm not entirely sure why you're getting the issue you're getting, you should understand that 'Input.Get$$anonymous$$ouseButton(0)' will return true each frame that the mouse button is held down (assu$$anonymous$$g it's inside the update function), so if you were to click the mouse button, it would most likely register it atleast twice, since you had the mouse clicked for more than 1 frame. You say you want to add velocity for as long as the mouse button is held down, how can you tell that when you release the mouse it does it twice if this is executing every frame?

avatar image tmalhassan AidanHorton · Oct 31, 2017 at 11:29 AM 0
Share

Hello @AidanHorton and thanks for your reply. As I said, I am actually getting the affect that I want by using Input.Get$$anonymous$$ouseButton(0). The problem that I am facing occurs only when I release the mouse button. And answering your question, the amount of force applied to the Y axis seems okay as long as the mouse button is held down, But once I release the mouse button, the added force applied seems to be twice as much (the distance that it travels is bigger). So in the end, I am only judging visually.

avatar image pako · Oct 31, 2017 at 11:29 AM 0
Share

@tmalhassan how do you check that you have released the mouse, and that after that release the force is added twice?

Because if you can check the mouse release accurately, you can then -maybe- add the force only "if the mouse has not been released".

avatar image tmalhassan pako · Oct 31, 2017 at 11:38 AM 0
Share

I am not checking it accurately, I am only judging visually. I don't think I can check it accurately though (since I am calling it every frame). Or is there a way to check it accurately?

avatar image jchester07 tmalhassan · Oct 31, 2017 at 11:52 AM 1
Share
 Debug.Log(controller.velocity);


avatar image pako · Oct 31, 2017 at 11:38 AM 0
Share

I just revised my answer to make sure that after the mouse button is released, force does not get added any more.

avatar image tmalhassan pako · Oct 31, 2017 at 12:00 PM 0
Share

Your new answer solved the issue. Although, I still don't get why it would behave like this using Input.Get$$anonymous$$ouseButton(0). Thank you :)

avatar image pako tmalhassan · Oct 31, 2017 at 01:32 PM 0
Share

I'm not sure why this happens, but I suspect that Input.Get$$anonymous$$ouseButton(0) is true for one more frame after you release the button.

You could probably verify that if you try:

  if (Input.Get$$anonymous$$ouseButton(0))
  {
      if(!Get$$anonymous$$ouseButtonUp(0)){
           Debug.Log("Button Released - Total Frames: " + Time.frameCount) //true for just 1 frame
        }

          Debug.Log("Total Frames: " + Time.frameCount)
 
      controller.velocity = Vector2.zero;
      controller.AddForce(new Vector2(0, 75f));
  }

Show more comments
avatar image
0

Answer by jchester07 · Oct 31, 2017 at 11:30 AM

Obviously, it will "Add Force" to the existing velocity of your controller.

If you want to maintain a constant velocity, then just set the velocity directly instead of setting it to zero then adding force.

Comment
Add comment · Show 2 · 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 tmalhassan · Oct 31, 2017 at 12:03 PM 0
Share

The reason why I am setting it to the velocity to 0, is because it would give it the affect of falling down once the mouse button is released. The way velocity works is that it will keep redusing till it reaches 0 (This will give the rigidbody a floating kind of effect). Unlike when you set it to 0, it would stop and immediately start falling down. Thanks a lot for your answer.

avatar image jchester07 tmalhassan · Oct 31, 2017 at 12:46 PM 0
Share

Well, I think logically it would still result in the same effect.

But since it's better to use AddForce than setting the velocity directly as also advised by Unity, then your code is good to go. And by using AddFoce you can set different force modes which is better.

avatar image
0

Answer by AidanHorton · Oct 31, 2017 at 11:38 AM

You are using AddForce when you could simply set the velocity. Try something like this, it should be more lightweight as well (but it may not exactly fit your use case):

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         controller.velocity = new Vector2(0, 75f);
     }
 }

What it does, is when you press the mouse button down, it sets the velocity to be 75f. It will stay like that until some external interaction of some kind. If you want to ensure it stays at 75f for the entire time you have your mouse button down, then simply change 'Input.GetMouseButtonDown' to 'Input.GetMouseButton'

Comment
Add comment · Show 2 · 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 tmalhassan · Oct 31, 2017 at 12:05 PM 0
Share

The problem with Input.Get$$anonymous$$ouseButtonDown(0) is that it will only add the force once until I release the mouse button and then click it again (Which isn't really the affect that I want). Regardless, thank you for your answer :)

avatar image AidanHorton tmalhassan · Oct 31, 2017 at 12:15 PM 0
Share

Forgive me, I don't think I still fully understand the issue, but I mentioned you could change it to 'Input.Get$$anonymous$$ouseButton' if you want it to constantly update, this would set the velocity to 75f while you have your mouse button held down. What you were doing before was a really convoluted way of setting the velocity, where you set the velocity to zero, then added a certain amount of force. Using controller.velocity sets the velocity, and it means there shouldn't be an issue with too much force being added, since you aren't adding force, simply setting it. If there is still an issue after that it must be an external issue elsewhere in the code where you add velocity.

avatar image
0

Answer by znickbar · Apr 29, 2020 at 07:10 PM

@tmalhassan You may try Input.ResetInputAxes(); it works for my scenario.

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

Follow this Question

Answers Answers and Comments

443 People are following this question.

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

Related Questions

How do I measure the velocity in Z-direction? 4 Answers

how to use onmousedown() or onmouseup() unity on local multiplayer/vs mode? 0 Answers

Multiple Cars not working 1 Answer

Want to move object slowly to where the mouse clicks? 1 Answer

Distribute terrain in zones 3 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