Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by SuperRaed · Mar 26, 2016 at 05:18 PM · uidrag-and-dropdrag objectsdraggable

Can I make a non UI gameObject draggable by implementing the IDragHandler interface?

I've been following an online tutorial where Ui Components are dragged by implementing the IDragHandler interface but when I tried to apply it to a non UI gameObject it's not working, is there a way to make it work and any helpful tutorials on the subject?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by troien · Mar 27, 2016 at 09:16 AM

You can.

You need to set up your eventsystem accordingly however.

To do this you need 2 things.

  1. An EventSystem

  2. A raycaster (A canvas uses a GraphicRaycaster, but you can also use Physics2DRaycaster or PhysicsRaycaster).

To make your object respond to events, simply add a PhysicsRaycaster to your camera, make sure you have an eventsystem somewhere in the scene, and that your MonoBehaviour implements IDragHandler (and make sure it has a collider obviously).

As for how to implement dragging, this is a simplified piece of code that allows you to drag in world x/z.

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class DragExample : MonoBehaviour, IDragHandler
 {
     public void OnDrag(PointerEventData eventData)
     {
         // Vector3.up makes it move in the world x/z plane.
         Plane plane = new Plane(Vector3.up, transform.position);

         Ray ray = eventData.pressEventCamera.ScreenPointToRay(eventData.position);
         float distamce;
         if (plane.Raycast(ray, out distamce))
         {
             transform.position = ray.origin + ray.direction * distamce;
         }
     }
 }

Comment
Add comment · Show 3 · 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 SweatyChair · Jan 09, 2018 at 03:59 AM 0
Share

best and easiest answer!

avatar image ngrimesdev · May 14, 2019 at 09:24 PM 0
Share

I know this is a bit old, but can you explain what's happening here, particularly with the plane and why it's necessary?

avatar image troien ngrimesdev · May 15, 2019 at 09:36 AM 1
Share

I'll try to explain this using words. Sinse my drawing skills are terrible... But I hope I'm clear enough so you can visualize it in your head yourself :)

First thing, to be sure you don't misunderstand. This Plane is not an actual GameObject that exists in the world, It is just a bit of data (position and normal) that represents a 2D plane on which we are dragging the object in a 3D world.


When dragging a 2d object in a 2d world, dragging is straightforward, as you can move the mouse over a 2d plane (in screen space) and the object moves along over the same 2d plane.


In 3d this can be more complicated, as you still move your mouse over a 2d plane (screen space). But the object you want to move has this extra dimension it might want to move in. If you would drag using the same plane as the camera, you can't move your object in this extra third dimension, and rotating the camera would mean you are suddenly moving the object on a different plane. This type of dragging could be what you want, but in most cases it is just very frustrating for the user, as having to perfectly rotate the camera in order to move an object in the direction you want is just really annoying.


Therefore we limit dragging to a fixed 2d plane, but unlike in a 2d world, this 2d plane doesn't have to align with the camera. $$anonymous$$eaning that if we rotate our camera, the plane on which we move our object doesn't change, making dragging much more intuitive. To do this we create an (imaginary/invisible) plane on the position of the object we are dragging with a 'normal' indicating which direction the plane is facing in world space. (Vector3.up is the most common one as it means you can drag the object along x/z (i.e. ground), but if you want to drag it along a different plane you can change this for your needs)


We then cast a ray from the camera, through the mousePosition and look at which point it intersects our imaginary plane, we then place our 3d object at the position where the ray intersects the plane.


There are other options ofcourse, but that all depends on how you want to drag the object. You could for instance raycast your terrain ins$$anonymous$$d, and move it along the point where you intersect your terrain. But I opted to give a example that works in practically all setups, as the OP didn't specify a setup nor how the objects should be dragged.

avatar image
-1

Answer by Geometrical · Mar 26, 2016 at 07:57 PM

No, you'll have to write up your own code structure.

Comment
Add comment · 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

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

57 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot drag-and-drop ui.text in inspector 1 Answer

Grid Inventory : Move Item & Slot Highlighting 2 Answers

How can I drag just one object, but not all the objects ? (Android) 1 Answer

Drag And Drop Window 1 Answer

Drag and drop picks an object too far away from mouse position 2 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