Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 snow2405 · Jul 12, 2019 at 10:03 PM · uiinstantiateraycastonmousedownonmousedrag

UI element blocking OnMouseDrag()?

Hello beloved community, I am sorry if this problem turns out to be dumb but I have now been stuck with it for over 3 hours, so I hope you can help me.


My scene

By clicking on a UI Image (with the Eventsystem OnPointerDown function), a 3D object gets spawned at the UI Image location. This Object can than be dragged around the scene. video: https://www.youtube.com/watch?v=8zik2eDTNwk&feature=youtu.be


The Problem

At the moment there are 2 Clicks needed. 1 Click to instatiate the object and 1 Click to drag it around. I would like to have that happen in one Click only, So that my OnDrag() function gets called with the Instatiate() function. So that you can basicly drag the object from the UI in to the scene.


my way of thinking & what dint work

I tried a lot of diffrent things for example calling the Drag function by script or changing the way the UI Image gets pressed/activated. But in the end there are always 2 clicks needed. This make not so much sense to me, because the second click can also be above the raycast-blocking Image but still activates the OnDrag function (see second part of the video). Is there a reason why the OnDrag function doesnt get called in the first Click? and how do I work around it.


scripts

I provided the scripts in a comment below, I dont think they are part of the problem but you never know. I appreciate every answer or also just some keywords which I can try to google for. As always thank you in advance, feel free to ask if something is not clear. The best wishes from Switzerland. snow2405.

Comment
Add comment · Show 1
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 snow2405 · Jul 12, 2019 at 10:06 PM 0
Share

Script for instatiating the Object:

  public void InstatiateDragable(int index)
     {
         GameObject gm = Instantiate(Prefabs[index], transform);
         SetPosToUiPos(uiButtons[index].transform.position, gm.transform);
         DragObject drag = gm.GetComponentInChildren<DragObject>();
         drag.On$$anonymous$$ouseDown();
         
     }

Script on the instatiated Object to drag it around:

 //function is public so I could try to call it from the other script
     public void On$$anonymous$$ouseDown()
     {
         mZCoord = Camera.main.WorldToScreenPoint(parent$$anonymous$$ove.position).z;
 
         // Store offset = gameobject world pos - mouse world pos
         mOffset = parent$$anonymous$$ove.position - Get$$anonymous$$ouseAsWorldPoint();
     }

   public void On$$anonymous$$ouseDrag()
     {
         Vector3 Newcord = new Vector3(0, (Get$$anonymous$$ouseAsWorldPoint() + mOffset).y, (Get$$anonymous$$ouseAsWorldPoint() + mOffset).z);
 
         parent$$anonymous$$ove.position = Newcord;  
     }
 }

1 Reply

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

Answer by ADiSiN · Jul 13, 2019 at 11:24 AM

Hi, @snow2405 !

Actually, I can't answer on your certain question about drag function, however I can suggest you scripts that you can see below to workaround. They are instantiating the object and move it with 1 click and also you can move object after instantiating.

Script for instantiating and move:

 using UnityEngine;
 
 public class InstaMove : MonoBehaviour
 {
     public GameObject[] prefabs;
     public Transform canvasParent;
 
     Transform t_CurrentMoveObj;
     bool b_IsMoving = false;
     Vector3 v3_MouseOffset;
 
     public void InstatiateDragable(int index)
     {
         /* Instantiate and assign to mouse position */
         GameObject gm = Instantiate(prefabs[index], Input.mousePosition, Quaternion.identity, canvasParent);
 
         /* Assign varaible so will be able to move later instantiated obj with this script */
         gm.GetComponent<MoveSenter>().instaMove_ = this;
 
         t_CurrentMoveObj = gm.transform;
         MoveObject(t_CurrentMoveObj);
     }
 
     private void Update()
     {
         /* Movin object at instantiate or when press mouse button down over object */
         if (b_IsMoving)
         {
             t_CurrentMoveObj.position = Input.mousePosition - v3_MouseOffset;
 
             /* Stop move when mouse button is up */
             if (Input.GetMouseButtonUp(0))
             {
                 b_IsMoving = false;
                 t_CurrentMoveObj = null;
             }
         }
     }
 
     public void MoveObject(Transform transformObj)
     {
         t_CurrentMoveObj = transformObj;
         b_IsMoving = true;
 
         /* Calculate mouse offset from center of moving obj */
         v3_MouseOffset = Input.mousePosition - t_CurrentMoveObj.position;
     }
 }

Script on instantiated object to contact with script that calculate movement:

 using UnityEngine;
 
 public class MoveSenter : MonoBehaviour
 {
     public InstaMove instaMove_;
 
     /* Set up event trigger system on mouse button down */
     public void AllowMove()
     {
         instaMove_.MoveObject(transform);
     }
 }

If you have any questions regarding to these scripts - feel free to ask ;)

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 snow2405 · Jul 13, 2019 at 08:46 PM 0
Share

Thank you very much, the scripts sadly didnt work out for, but I think thats jsut because I work with an orthographic camera. However you really showed me the problem I really had, which was that the OnDrag function was always only called one cklick after the Instatiate function. So looking back it now seems obvious and I got it to work. So thank you again for showing me the Solution and I hope you enjoy the rest of your day.

$$anonymous$$uch love from Switzerland snow2405

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

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

EventSystem: how to highlight instantiated button? 1 Answer

Instantiate as a child? 3 Answers

Instantiating into UI objects 1 Answer

Unity UI - Select with Raycast (Screenspace - Camera) 0 Answers

How can I instantiate parented ui objects above previously instantiated children? 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