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 tsondhi2006 · Dec 01, 2017 at 05:30 AM · mouseclickdrag-and-dropdraggingdrag and dropdrag objects

spawn and drag game object without leaving mouse button

I want to make a system where I can spawn the gameobject on mouse click and without leaving the mouse, i am able to drag it to whatever position i want (ie spawn and drag). I have tried searching throughout net but found no solution. Right now, I am using a button to spawn the desired object and then drag the object.

Thanks in advance

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 Darkforge317 · Dec 01, 2017 at 06:14 AM 0
Share

If you let go of the mouse do you want the object to be placed or destroyed?

avatar image tsondhi2006 · Dec 01, 2017 at 06:21 AM 0
Share

I want object to be placed if i leave the object

avatar image Darkforge317 tsondhi2006 · Dec 01, 2017 at 07:21 AM 0
Share

I've written code for this. Check my answer :) Hopefully this is what you wanted.

Once you set up the script like I said in my answer, all you have to do is click and hold, then you'll be able to drag the object wherever you want to. If you let go of the left mouse button then the object is placed and can't be dragged anymore.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Darkforge317 · Dec 01, 2017 at 07:18 AM

I made some code for this. It is not very elegant or pretty, it could use a lot of optimization... but it does work.

First, make a new layer called "SpawnedObject". alt text
Second, add this script to any object in the game.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnAndDrag : MonoBehaviour
 {
     // Drag the prefab you would like spawned into this variable
     //  in the Unity inspector
     [SerializeField]
     GameObject objectToSpawn;
 
     // Reference to the instantiated object
     GameObject spawnedObject;
 
     // Stores the information of where our raycast hits
     RaycastHit hit;
 
     // The ray we're going to cast with our left click
     Ray ray;
 
     // The layer number for the "SpawnedObject" layer and 
     //  a layer mask that will tell the ray to ignore that layer.
     int spawnedObjectLayer, spawnedObjectMask;
 
     // Use this for initialization
     void Start()
     {
         // Get the number for the "SpawnedObject" layer.
         spawnedObjectLayer = LayerMask.NameToLayer("SpawnedObject");
 
         // Create a layer mask that ignores this layer
         spawnedObjectMask = ~(1 << spawnedObjectLayer);
     }
 
     // Update is called once per frame
     void Update()
     {
         // Create a ray so we can raycast it later
         ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
 
         // If we click/hold the left mouse
         if(Input.GetMouseButton(0))
         {
             // Raycast from our Camera to our mouse position
             // If the ray hits an object that does not have a layer of "SpawnedObject"
             if (Physics.Raycast(ray, out hit, 999999f, spawnedObjectMask))
             {
                 // If we don't have a spawnedObject yet.
                 if (spawnedObject == null)
                 {
                     // Spawn our object at the mouse's position
                     spawnedObject = Instantiate(objectToSpawn, hit.point, Quaternion.identity);
 
                     // Set it's layer to "SpawnedObject" so that our raycast ignores it
                     spawnedObject.layer = spawnedObjectLayer;
                 }
                 // If we do have a spawnedObject
                 else
                 {
                     // Changes the position of our spawned object to
                     //  where the mouse's position is.
                     spawnedObject.transform.position = hit.point;
                 }
             }
         }
         // If we let go of the left mouse button
         else if (Input.GetMouseButtonUp(0))
         {
             // Sets the object's layer back to "Default"
             spawnedObject.layer = 0;
 
             // Gets rid of our reference for the spawned object so
             //  that we can't drag it anymore.
             spawnedObject = null;
         }
     }
 }
 


Third, click on the object you put the script on and drag the prefab of the object you want to spawn into the slot called "Object To Spawn" on the script. alt text


untitled.png (54.8 kB)
capture.png (24.5 kB)
Comment
Add comment · Show 6 · 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 tsondhi2006 · Dec 01, 2017 at 07:35 AM 0
Share

the object that ive put the script on, is the object which will be clicked and dragged??

and layer of that object will be SpawnedObject??

It is showing null reference exception on line 70th.

avatar image Darkforge317 tsondhi2006 · Dec 01, 2017 at 07:54 AM 0
Share

The object that you put the script on is NOT the object that you will be dragging.
It's the object that you want to Instantiate (spawn) when you click.
You can make the layer of the object you want to spawn "SpawnedObject" if you want to. It won't break anything.

The reason why it's showing a null reference is most likely because you didn't drag the object that you want to spawn into the "Object To Spawn" slot on the second image.

avatar image tsondhi2006 Darkforge317 · Dec 01, 2017 at 08:02 AM 0
Share

i have a sprite with which i have attached your script and another a prefab assigned to "Object to spawn" variable which i want to spawn and the sprite is at layer "SpawnedObject" but still the error is co$$anonymous$$g on mouse up (doesnt matter wherever my mouse up is on the scene)

alt text

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

72 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

Related Questions

Following instruction for drag and drop 3d object unity but not working 1 Answer

Drag and drop picks an object too far away from mouse position 2 Answers

Collision while dragging in Canvas with IDragHandler 0 Answers

Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position 0 Answers

NGUI Mouse position doesn't match with dragged object 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