2d item dragging unity

public class followMouse : MonoBehaviour
{

    bool canDrag = false;

    Vector3 itemPos;
    void Update()
    {
        transform.position = Input.mousePosition;
        if (!Input.GetMouseButton(0))
        {
            canDrag = false;
        }

    }
    
    void OnTriggerStay2D(Collider2D other)
    {
        if (other.CompareTag("itemIcon"))
        {
            if (Input.GetMouseButton(0))
            {
                canDrag = true;
                other.transform.position = Input.mousePosition;
            }
            
        }
    }



}

4
2
Mark Joseph 100 points

                                    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragAndDrop : MonoBehaviour
{

    private bool isDragging;

    public void OnMouseDown()
    {
        isDragging = true;
    }

    public void OnMouseUp()
    {
        isDragging = false;
    }

    void Update()
    {
        if (isDragging) {
            Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
            transform.Translate(mousePosition);
        }
    }
}

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Unity 2d how to be able to drag a gameobject and move it unity block dragging 2d unity drag 2d object drag and drop unity 2d c# dragable 2d unity scrip drag and drop 2d unity unity drag and drop gameobject 2d unity 2d draggable object unity unity draggable 2d object unity 2d movement drag 2d drag and drop script unity how to drag and object in unity 2d drag and drop unity 2d click and drag in unity 2d unity 2d linear drag unity drag and drop 2d how to drag a box with player in unity 2d how to drag an box with player in unity 2d how to drag an object with player in unity 2d drag and drop object unity 2d drag unity 2d linear drag unity 2d c# unity 2d drag drag 2d object unity how to drag object in unity 2d drag in 2d canvas unity unity 2d drag and drop unity drag and drop 2d object drag a gameobject in unity 2d click and drag unity 2d unity 2d drag object script drag object unity drag gameobject in unity 2d unity2d onmousedrag drag and place unity 2d draging object in unity unity drag camera drag and drop game in unity unity draggable object UNITY 2d mobile drag and drop 2d drag object with character unity 2d drag object unity drag unity 2d to place object drag and drop in unity 2d make drag and drop unity 2d how to drag 2d object unity how to drag and drop an object in unity 2d darg and drop object in unity 2d unity drag object 2d unity click and drag object 2d unity dragable object 2d unity drag object with mouse 2d dragging objects 2d unity 2d dragging objects unity unity2d how to make objects draggable unity drag 2d objects with mouse unity click and drag 2d unity sprite drag and drop unity move object mouse drag 2d make 2d box draggable unity drag and drop unity tutorial 2d how to make a 2d object draggable 2d item dragging unity click and drag and object unity 2d unity draggable object 2d drag things unity 2d drag in rigidbody unity unity 2d drag object with mouse drag object unity 2d
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source