• 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 NGKrush1 · Jul 01, 2013 at 01:08 PM · c#draganddrop

drag and drop objs -> returning obj to original pos (C#)

Hi,

I'm working on a drag & drop script which functions like this: mousebutton down on an object selects an object holding mousebutton down, moves it releasing mousebutton drops it.

Now I'm trying to make it so that the object will only drop at dropped position if it is dropped on a specific object, and will return to it's original position if it is not. I am able to do either, but not both within the same code.

Anyone see what I'm doing wrong? objects to dragged are tagged as "bomb" and objects they can be dragged to are tagged "grid".

 void Update () {
         
         if (Input.GetMouseButtonDown(0) && target == null)
         {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                         
             if (Physics.Raycast(ray,out hit,1000))
             {
                 Debug.Log("hit tag: "+hit.transform.tag);
                 if (hit.transform.tag == "bomb")
                 {
                     originalBombPos = hit.transform.position;
                     target = hit.transform;
                     hit.transform.GetComponent<SphereCollider>().enabled = false;
                 }
             }
         }
         else if (Input.GetMouseButtonUp(0) && target != null)
         {
             
             if (Physics.Raycast(ray,out look,1000))
             {
                 Debug.Log("look tag: "+look.transform.tag);
                 if (look.transform.tag != "grid")
                 {
                     //return the bomb to it's original position
                     hit.transform.position = originalBombPos;
                 }
             }
             //else drop it at current position        
             target = null;
             hit.transform.GetComponent<SphereCollider>().enabled = true;
         }
         
         if (target != null)
         {
             Vector3 moved = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f);
             Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(moved);
             hit.transform.position = realWorldPos;
         }        
         
     }
Comment
Add comment · Show 2
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 NGKrush1 · Jul 02, 2013 at 12:53 PM 0
Share

After some more testing it seems the raycast with out "look", that I call on in the Input.Get$$anonymous$$ouseButtonUp(0) method actually gets it's info on mousedown and not mouseup. I don't know why this isn't working. I think solving this, will solve my problem.

avatar image Immanuel-Scholz · Jul 02, 2013 at 12:59 PM 0
Share

is there any particular reason why you don't use the built-in functions like void On$$anonymous$$ouseDrag() in scripts attached to your target? They seem to be perfectly suited for your problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 02, 2013 at 01:16 PM

In the Input.GetMouseButtonUp() logic, you are not constructing a new ray for the new position of the mouse. You are using the ray you constructed for the Input.GetMouseButtonDown() . Just add the same ray code on line 20:

  ray = Camera.main.ScreenPointToRay(Input.mousePosition);
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
avatar image
0

Answer by NGKrush1 · Jul 02, 2013 at 01:43 PM

Solved. I forgot to acces:

ray = Camera.main.ScreenPointToRay(Input.mousePosition);

in the onmouseup method too, so it wouldn't check the new mouseposition.

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.

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Custom Editor DragAndDrop for List 1 Answer

Drag And Drop with Perspective Camera 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