• 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 atwilliamb · Dec 31, 2018 at 10:02 PM · raycastcolliderprefabdrag-and-drop

Script is being applied to each prefab instead of one

Hi, sorry for the bad title, there is this script that allows objects to be dragged and dropped using the mouse, however it is dragging and dropping anything with a collider attached.

 {
     private bool mouseState;
     private GameObject prefab;
     public Vector3 Screen;
     public Vector3 offset;
 
     
     void Start()
     {
         
     }
 
     
     void Update()
     {
         
         if (Input.GetMouseButtonDown(0))
         {
 
             RaycastHit hitInfo;
             prefab = GetClickedObject(out hitInfo);
             if (prefab != null)
             {
                 mouseState = true;
                 Screen = Camera.main.WorldToScreenPoint(prefab.transform.position);
                 offset = prefab.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Screen.z));
             }
         }
         if (Input.GetMouseButtonUp(0))
         {
             mouseState = false;
         }
         if (mouseState)
         {
             
             var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Screen.z);
 
             
             var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
 
             
             prefab.transform.position = curPosition;
         }
     }
 
 
     GameObject GetClickedObject(out RaycastHit hit)
     {
         GameObject prefab = null;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
         {
             prefab = hit.collider.gameObject;
         }
 
         return prefab;
     }
 
 
 }

I have tried making the "GameObject prefab" variable public and applying it the desired prefab, however, every object is still able to be dragged and dropped. How can I make this script apply to one gameobject.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ady_M · Jan 01, 2019 at 04:32 PM

Your script is not checking if it's a draggable object.

if (prefab != null) is simply asking if you successfully clicked on any object that has a collider.


Currently, you have one instance of a script that raycasts to find out which object is clicked.

There's another solution: Try giving every draggable object a script that has the built-in methods, OnMouseDown and OnMouseUp (Raycast is not needed at all in this case). This script should have the mouseState, offset, etc. and the script should only be allowed to move the object it is attached.


If you insist on managing the drag & drop system the way you're doing it, it can of course be done. While explaining this, I will refer to your original script as the DragAndDropManager script.

You'll have to find a way to distinguish between draggable and non-draggable objects, for example by using tags or by attaching a Draggable script to the objects and do if (clickedObject.GetComponent () != null) inside the GetClickedObject method of the DragAndDropManager script. The Draggable script could simply contain an empty class and is used only to determine whether the object is draggable.


Finally, I would not recommend naming a variable "prefab" if it is also used to reference a specific object.

Comment
Add comment · Show 1 · 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 WilliamFerdinand · Jan 06, 2019 at 07:42 AM 0
Share

Ah, got it, I completely forgot about that function! Thank you!

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

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

Dragging an object in iOS (UnityScript) 3 Answers

Ignore raycast from specific object? 1 Answer

Raycast avoiding spesific colliders 2 Answers

Raycast not updating inside a trigger collider 0 Answers

Touch script using raycast on a collider.. :( 0 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