Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Evil_Weevil · Oct 29, 2013 at 10:55 AM · click objects

GetMouse doesn`t work correctly

Here is my code:

 if(Input.GetMouseButtonUp(0))
       {
      rigidbody.AddForce(Vector3(power * 1.5,power,0));
       }

It`s attached to the object (Sphere), so everytime I click on it I wish my sphere to jump. It jumps indeed — but not only when I click it on. but when I click anywhere as well.

What am I doing wrong?

Thanx in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Oct 29, 2013 at 10:59 AM

I think you are after this :

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseUp.html

otherwise you need a raycast check first:

 RaycastHit hit;
 Ray ray = Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 Physics.Raycast(ray,out hit);
 if(Input.GetMouseButtonUp(0)&&hit.collider.name == "ObjectName"){
     // AddForce
 }
Comment
Add comment · Show 4 · 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 Evil_Weevil · Oct 29, 2013 at 11:11 AM 0
Share

Thanks! I$$anonymous$$HO its illogical. The script is IN the object. Why is the engine checking the whole gameworld?

avatar image fafase · Oct 29, 2013 at 11:24 AM 0
Share

Actually sorry to say but your reasoning is not logic. The method is called Get$$anonymous$$ouseButtonUp so it does what is says. It does not matter whether you are doing on the object or not. On$$anonymous$$ouseUp on the other hand does exactly what you want. It is an event called when the mouse is up on the the object (the On at the front does not mean on the object it is a convention for event na$$anonymous$$g).

But for instance, think of a GUI system, you have object for the buttons (GUITexture), the script is not on any of the objects, but it should report anyway. Or you would have to have as many scripts as buttons. See the trick?

avatar image Tomer-Barkan · Oct 29, 2013 at 11:25 AM 0
Share

It's not illogical, Input.Get$$anonymous$$ouseButton() is a static function that checks whether the mouse was clicked, it is not associated with any specific object so it doesn't check if that specific object was clicked.

$$anonymous$$onoBehaviour.On$$anonymous$$ouseUp on the other hand, is associated with a specific object, hence it can check whether that specific object was pressed.

avatar image Evil_Weevil · Oct 29, 2013 at 11:28 AM 0
Share

$$anonymous$$any thanks! Now I shall know the difference.

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Click on two objects to render third 2 Answers

How do I let the player interact with objects close to them? 1 Answer

How to left click to rotate a box in a same direction in Unity 2D? 1 Answer

Make OnMouse functions ignore GameObjects that pass in front of them 1 Answer

How to click only one once? 0 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