- Home /
How to make an object appear only when I click and not when I drag
How to make an object appear only when I click and not when I drag
void Start ()
{
col = GetComponent<Collider>();
handler = new newClass();
visible = false;
newPosition = canv.transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0) )
{
isDown = true;
handler.ray = handler.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (col.Raycast(handler.ray, out hit, 100.0f))
{
// handler.Delete(gameObject);
canv.transform.position = new Vector3(transform.position.x, transform.position.y + 2f, transform.position.z);
}
}
if (Input.GetMouseButton(0))
canv.transform.position = newPosition;
}
The problem is that on I get MouseButtonDown the MouseDrag(Mousebutton) is also registered, How I can make to not register it, so when I drag it will go with mousebuttondrag, and when I click it will go only with mousebuttonDown
Answer by Utamaru · Mar 20, 2015 at 09:26 AM
I would recommend you to use new unity EventSystems. For example, if you want to catch clicks on your gameObjects, just implement IPointerClickHandler interface in script, attached to your gameObject. And then, when click on your object happens, OnPointerClick message will be called. You also need to add PhysicsRaycaster component to your main camera. Don't forget to do that.
Example code:
public class FindableObject : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick (PointerEventData eventData)
{
Debug.Log ("OnPointerClick " + gameObject.name);
}
}
This event will not be called if you drag element. For drag event you can implement another interface. Just check EventSystems' interfaces in documentation
Answer by MiniFish · Mar 19, 2015 at 07:18 AM
this script looked like you are trying to drag and place something. have you looked into using Input.GetMouseButtonUp()?
ButtonUp will give an event when releasing the button, but I need when I click it.
hmm, what about recording the initial input position during buttonDown and then compare the input position in getButton to deter$$anonymous$$e if the mouse did move? if input did move, then you do whatever else you wanted to do, otherwise just ignore.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I separate OnMouseDown() from OnMouseDrag() 0 Answers
Irregular jumping 1 Answer
C# GetMouseButtonUP on specific object ??? 0 Answers
Mouse drag with specific angle 1 Answer
Raycast repeats multiple times 2 Answers