• 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
0
Question by oneted_7 · Mar 14, 2020 at 11:22 PM · listvector2drag and drop

drag drop a object into list objectes

hey all. I wanna drag-drop an object into a list of objects . for example I have a blue object and wanna drag and drop into the w$$anonymous$$te piece. I wrote code like t$$anonymous$$s. but a little bug is in cuz a lot of t$$anonymous$$ngs should check-in "if" and sometimes my blue object going back to $$anonymous$$s first position (i t$$anonymous$$nk for a lot of conditions and checking a lot of distance, system going to else of foreach ). so... I t$$anonymous$$nk need a better solution .if u have any idea help mealt text

   foreach (var piece in Piece)
     {
         if (Vector2.Distance(CurrentColor.transform.position,piece.transform.position)<0.4f &&  piece.GetComponent<Colorslot>().Entered && !CurrentColor.GetComponent<DragDrop>().dragging )
         {
             piece.layer = CurrentColor.layer;
             piece.GetComponent<Colorslot>().ColorInPosition = true;
             CurrentColor.transform.position = piece.transform.position;
             CurrentColor.transform.position=new Vector3(CurrentColor.transform.position.x,CurrentColor.transform.position.y,CurrentColor.transform.position.z-10f);
             CurrentColor.GetComponent<DragDrop>().draglock = true;
             NextColor.transform.position = Vector2.Lerp(NextColorPos.transform.position,CurrentColorPos.transform.position, 0.3F*Time.fixedDeltaTime);
             
             CurrentColor = NextColor;
             CurrentColor.GetComponent<DragDrop>().draglock = false;
             nextcolor = false;
             playerturn = false;
             

         }else   if ( !CurrentColor.GetComponent<DragDrop>().dragging && !piece.GetComponent<Colorslot>().Entered)
             CurrentColor.transform.position =
                 Vector2.Lerp(CurrentColor.transform.position, CurrentColorPos.transform.position, 3f);
     }


Comment
Add comment · Show 3
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 oneted_7 · Mar 15, 2020 at 08:18 AM 0
Share
avatar image ShadyProductions · Mar 15, 2020 at 08:31 AM 1
Share
avatar image oneted_7 ShadyProductions · Mar 15, 2020 at 11:26 AM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by metalted · Mar 15, 2020 at 01:27 PM

I've created t$$anonymous$$s little example script to give you an idea how to tackle the problem. The way you are doing it now, by checking distance for every tile in loops is very memory heavy. Running these heavy loops is not useful for when you only really need one of the tiles in the grid. T$$anonymous$$s is more used for when you want to change a lot or even all the tiles. Like a ColorReset() for your tiles for instance. So as to only single out a couple objects that we actually interact with, we can use a raycast to see what we are pointing at and figure it out from there.


 using UnityEngine;
 
 public class ObjectGrid : MonoBehaviour
 {
     public GameObject dragObject;
     public Vector3 dragStartPosition;
     public int previousLayer;
     private void TryDrag()
     {
         RaycastHit $$anonymous$$t;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out $$anonymous$$t))
         {
             if($$anonymous$$t.collider.tag == "Drag")
             {
                 dragObject = $$anonymous$$t.collider.gameObject;
                 dragStartPosition = $$anonymous$$t.point;
                 previousLayer = dragObject.layer;
                 dragObject.layer = LayerMask.NameToLayer("Ignore Raycast");
             }
         }
     }
 
     private void Place()
     {
         RaycastHit $$anonymous$$t;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out $$anonymous$$t))
         {
             bool canPlace = $$anonymous$$t.collider.tag == "Place";
             dragObject.transform.position = canPlace ? $$anonymous$$t.point : dragStartPosition;           
             dragObject.layer = previousLayer;
             dragObject = null;
         }
     }
 
     void Update()
     {
         if (Input.GetMouseButton(0))
         {
             TryDrag();
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             if(dragObject != null)
             {
                 Place();
             }            
         }
 
         if (dragObject != null)
         {
             RaycastHit $$anonymous$$t;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out $$anonymous$$t))
             {
                 Vector3 mousePosition = $$anonymous$$t.point;
                 mousePosition.y = dragStartPosition.y;
                 dragObject.transform.position = mousePosition;
             }
         }
     }
 }
 



You can use t$$anonymous$$s script right away if you have a top down camera, a plane as a background, and 2 objects, one with tag "Place" and one with tag "Drag". Place the script on an object in the scene. When the mouse button is pressed, a raycast will check if the mouse position is on top of an object with tag "Drag". If it is, move that object with the mouse as long as the button is pressed. Also move it to another layer so that the raycast won't $$anonymous$$t it w$$anonymous$$le we are holding it. When the button is released, send out a ray again, but now to check if there is a "Place" tagged object. If there is, place that object on the $$anonymous$$t.point, else place it back from where you pick it up.

Comment
Add comment · Show 4 · 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 oneted_7 · Mar 17, 2020 at 11:04 AM 0
Share
avatar image metalted oneted_7 · Mar 19, 2020 at 10:02 AM 1
Share
avatar image oneted_7 metalted · Mar 19, 2020 at 02:51 PM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 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 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 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

drag drop a object into list objectes 0 Answers

A node in a childnode? 1 Answer

Jigsaw Drag and Drop Game? Help! 0 Answers

initialising a list with values, and adding a range to a list 2 Answers

How to make an IList of a certain type? 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