- Home /
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.
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
}
Thanks! I$$anonymous$$HO its illogical. The script is IN the object. Why is the engine checking the whole gameworld?
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?
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.
$$anonymous$$any thanks! Now I shall know the difference.
Your answer
Follow this Question
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