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!