• 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
1
Question by catwoman · Oct 30, 2012 at 11:53 PM · instantiatemousedragonmousedown

OnMouseDown: instantiate a prefab and keep it dragged

Hi all, I don't know if this kind of question has already been asked.

So I have a 3D Text with a script on it. The desirable behavior is the following:

on the OnMouseDown event, an object should be instantiate in the current position of the mouse. This part is working correctly: a prefab is instantiating in the mouse position.

When the mouse button is still Down I'd like to drag the gameObject already created, in order to move it in another point of the space, until the mouse button return Up.

So far the gameObject is created, but I need to click on the object a second time, in order to start the Drag event.

Is it clear the situation? Can you suggest me some ideas?

Thanks thanks thanks!

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

2 Replies

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

Answer by Seth-Bergman · Oct 31, 2012 at 12:31 AM

In the future, try to post the existing script to start with...

Anyway, here's how to do this--

basically, when you create the object, you are storing its reference in some var, then use that to access its position.. You can use GetMouseButtonDown,GetMouseButton, and GetMouseButtonUp to keep track of the current state of everything:

 var objectToInstantiate : GameObject;
 private var myCurrentObject : GameObject;
 
 function Update(){
 if(Input.GetMouseButtonDown(0)){
 myCurrentObject = Instantiate(objectToInstantiate,Input.mousePosition,Quaternion.identity);
     }
 if(Input.GetMouseButton(0) && myCurrentObject){
 myCurrentObject.transform.position = Input.mousePosition;
     }
 if(Input.GetMouseButtonUp(0) && myCurrentObject){
 myCurrentObject = null;
     }
 }

this is a simple example of how this could work, hope this helps!

EDIT: sorry, just noticed the object is a textmesh.. my above answer uses the input mouse position directly, which might work for say, a GUI element, which uses similar coordinates.. however, for a normal gameobject, we would need to cast the mouse position to a world position:

 var objectToInstantiate : GameObject;
 private var myCurrentObject : GameObject;
 
 function Update(){
 if(Input.GetMouseButtonDown(0)){
 myCurrentObject = Instantiate(objectToInstantiate, camera.ScreenToWorldPoint(Input.mousePosition),Quaternion.identity);
     }
 if(Input.GetMouseButton(0) && myCurrentObject){
 myCurrentObject.transform.position =  camera.ScreenToWorldPoint(Input.mousePosition);
     }
 if(Input.GetMouseButtonUp(0) && myCurrentObject){
 myCurrentObject = null;
     }
 }

for example (this script would be attached to your camera.. ) also, you may need to adjust the z position in this scenario, which could be done directly after setting the rest, by adding the line:

 myCurrentObject.transform.position.z = 10;   // or whatever

immediately after setting the position in GetMouseButton, and also after the Instantiate line..

 ...
  if(Input.GetMouseButton(0) && myCurrentObject){
     myCurrentObject.transform.position =  camera.ScreenToWorldPoint(Input.mousePosition);
     myCurrentObject.transform.position.z = 10; 
         }
 ...
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 catwoman · Nov 01, 2012 at 11:07 AM

Thanks, great! It seems to work!

I'm having only some issues with the z position... but i'll figure it out :)

Thanks!

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

10 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

Related Questions

Instantiate and Scale with Mouse Click. 1 Answer

How to fix mouse click and drag hang in Unity 5.2.3.p2 OSX 1 Answer

How to detect when the mouse button is being held down while the cursor is inside a collider? 1 Answer

I want to disable mouse movement (i.e mouse drag) when animation is playing. 0 Answers

Checking if object intersects? 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