Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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
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 white piece. I wrote code like this. but a little bug is in cuz a lot of things should check-in "if" and sometimes my blue object going back to his first position (i think for a lot of conditions and checking a lot of distance, system going to else of foreach ). so... I think 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

any help??

avatar image ShadyProductions · Mar 15, 2020 at 08:31 AM 1
Share

your question is way too vague for us to even understand what's going on.

avatar image oneted_7 ShadyProductions · Mar 15, 2020 at 11:26 AM 0
Share

I wanna drop an object into white places. problem is sometimes when I try to drop into those , it back to the first position and doesn't transform to the white position. ID$$anonymous$$ exactly but cuz a lot condition is in if in foreach and cant calculate fast .some place has a problem. if I drop into another it will work. also if I check with the next color it doesn't work. I made a place with instantiating and added to a list . I also used foreach for checking distance of dragged object.

1 Reply

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

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

I've created this 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. This 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 hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             if(hit.collider.tag == "Drag")
             {
                 dragObject = hit.collider.gameObject;
                 dragStartPosition = hit.point;
                 previousLayer = dragObject.layer;
                 dragObject.layer = LayerMask.NameToLayer("Ignore Raycast");
             }
         }
     }
 
     private void Place()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             bool canPlace = hit.collider.tag == "Place";
             dragObject.transform.position = canPlace ? hit.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 hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit))
             {
                 Vector3 mousePosition = hit.point;
                 mousePosition.y = dragStartPosition.y;
                 dragObject.transform.position = mousePosition;
             }
         }
     }
 }
 



You can use this 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 hit it while 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 hit.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

I like your idea. but have something I can't understand and something is wrong. problems: I wanna if object didn't drop to tiles, back to first position but it don't work (dragObject.transform.position = canPlace ? hit.point : dragStartPosition; ) I think it cuz this code also I didn't understand that question in code. second tiles`s tag and layer wouldn't change.
I define collider for all objects and tiles. i put a debug.log into here

 `             if(hit.collider.tag == "Drag")
              {Debug.Log("ok");
                  dragObject = hit.collider.gameObject;
                  dragStartPosition = hit.point;
                  previousLayer = dragObject.layer;
                  dragObject.layer = Layer$$anonymous$$ask.NameToLayer("Ignore Raycast");
              }`

but it didnt work .what is problem ?

avatar image metalted oneted_7 · Mar 19, 2020 at 10:02 AM 1
Share

Alright i see. The question mark in the code is part of the ternary operator. It's shorthand for a simple if else statement (and of course much more, but thats not important right now). It is possible to directly convert the ternary operator into an if else:

 if (Physics.Raycast(ray, out hit))
 {
     //When we raycast towards the mouse position, do we detect a collider with tag "Place" ?
     bool canPlace = hit.collider.tag == "Place";
     //If canPlace is TRUE, so when a "Place" tag is detected, then the new position of the dragged object is the hit position of the raycast. If canPlace is FALSE, then the position of the dragged object is the position that it started from.
     dragObject.transform.position = canPlace ? hit.point : dragStartPosition;     
     //Set the layer of the dragged object back to the layer it was on before we picked it up.
     dragObject.layer = previousLayer;
     //We are not dragging an object anymore, so set it to null.
     dragObject = null;
 }
 
 //The equivalent if else version:
 if (Physics.Raycast(ray, out hit))
 {
     if(hit.collider.tag == "Place")
     {
         dragObject.transform.position = hit.point;
     }
     else
     {
         dragObject.transform.position = dragStartPosition;
     }
     
     dragObject.layer = previousLayer;
     dragObject = null;    
 }


Also, about your second problem. The tag isn't supposed to change, only the layer does. So if the code you provided doesn't work, it is probably because the object you are trying to drag with the mouse doesn't have the tag "Drag". It doesn't matter what the tag is, as long as the tag you set on the object and the tag you use in code are the same. Because in case your tag is NOT "Drag", the code in the if(hit.collider.tag == "Drag"){} block just won't run.

avatar image oneted_7 metalted · Mar 19, 2020 at 02:51 PM 0
Share

ty a lot bro for your help. I edit a little your code, I understood when I use spirit 2d,hi.collider.tag doesn't work so I test for a cube and it work. I delete try drag and only with my drag-drop script in my object and onmouseup solved that. changing place method help alot . thanks again

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.

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

A node in a childnode? 1 Answer

drag drop a object into list objectes 0 Answers

List of Vector2 - Madness looms in simple script 1 Answer

Move the player smoothly from a list of vector2's 1 Answer

How to make a function that runs a loop x amount of times (Determined by parsed int) 1 Answer


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