Home > database >  I wanted to move an Object with my Cursour
I wanted to move an Object with my Cursour

Time:03-01

I want to move an 2d Object with my Mouse but the Script doesnt work. Can someone help?

public GameObject selectedTroup;

    void Start () 
    {
        
    }
   
    void Update()
    {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetMouseButtonDown(0))
        {
            Collider2D overlapObject = Physics2D.OverlapPoint(mousePos);

            if (overlapObject)
            {
                selectedTroup = overlapObject.transform.gameObject;
                Debug.Log("Hi");
            }

            if (Input.GetMouseButtonUp(0) && selectedTroup)
            {
                selectedTroup = null;
            }
        }
    }

CodePudding user response:

GetMouseButtonDown(): just works one shojust when you click mouse down GetMouseButtonUp():just works one shojust when you take your finger from mouse GetMouseButton(): works as long as you don't take your finger off the mouse. So if you want to move some thing I recommend you to use GetMouseButton() to able to move your object.

  • Related