- Home /
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
If you let go of the mouse do you want the object to be placed or destroyed?
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.
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".
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.
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.
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.
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)
Your answer
Follow this Question
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