- Home /
Drag 3d objects on a 3d scenario?
Im using a RTS style camera (55°), and I manage to do a click on a UI button to instantiate a prefab at my mouse position with this code.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
isInstantianting = false;
if (Physics.Raycast(ray, out hit))
{
Instantiate(initiateGO, hit.point, Quaternion.identity);
}
And then I wanted to drag the prefab with my mouse, so I found this code here.
using UnityEngine;
public class DragTest2:MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
But what I really need is that the prefab follow my mouse without me having to hold a mouse button. Like ... I click the UI button to instantiate a prefab and then the prefab will be at the mouse position untill I click at the scenario to drop the prefab there, and I want the prefab to be always on top of the plane without going thru or high up to the sky whilst im dragging it.
Thx already!
Answer by Christian-Lemos · Mar 21, 2016 at 11:25 PM
Hi
try this one:
using UnityEngine;
using System.Collections;
public class UIInstantiate : MonoBehaviour
{
public GameObject machineGun;
GameObject instantiatedMachineGun;
bool instanteateStep1;
bool instanteateStep2;
bool instanteateStep3;
Vector3 wantedPos;
void Start ()
{
instanteateStep1 = true;
instanteateStep2 = false;
instanteateStep3 = false;
}
void Update()
{
Step1 ();
Step2 ();
Step3 ();
}
void Step1()
{
//I used a key. You only need to adapt it to your UIbutton
if(Input.GetKeyDown(KeyCode.A) && instanteateStep1 == true)
{
RaycastHit hit1;
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray1, out hit1, 100) )
{
instantiatedMachineGun = (GameObject)Instantiate (machineGun, hit1.point, Quaternion.identity);
instanteateStep1 = false;
instanteateStep2 = true;
}
}
}
void Step2()
{
RaycastHit hit2;
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
if (instanteateStep2 == true)
{
if(Physics.Raycast (ray2, out hit2, 100))
{
if(hit2.collider.gameObject == instantiatedMachineGun)
{
if(Input.GetButton ("Fire1"))
{
instanteateStep2 = false;
instanteateStep3 = true;
}
}
else
{
wantedPos = new Vector3 (hit2.point.x, hit2.point.y, hit2.point.z);
instantiatedMachineGun.transform.position = wantedPos;
if(Input.GetButton ("Fire1"))
{
instanteateStep2 = false;
instanteateStep3 = true;
}
}
}
}
}
void Step3()
{
if (instanteateStep3 == true)
{
instanteateStep1 = true;
instanteateStep3 = false;
instantiatedMachineGun.transform.position = wantedPos;
}
}
}
I made and tested this script assigning it to an empty gameobject (like a gameController).
I'm sorry if this dont work for you.