- Home /
Instantiate gameobject at mouseposition 2d
I know this question has been answered many times, but no matter what I do, the object keeps instantiating at the middle of the screen, but slightly towards where my mouse is. Here's what I've tried.
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
Instantiate(go, worldPoint, Quaternion.identity);
I have also tried using the ScreenPointToRay and storing the origin in a vector3
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cam.nearClipPlane));
Instantiate(go, new Vector3(ray.origin.x, ray.origin.y, 1f), Quaternion.identity);
I've also used the basic Input.mousePosition and pretty much every other answer, any idea or am I just stupid and missing something simple?
my near clip plane is 0.3 and my camera's z position is -11
Answer by jmfp92 · Jun 23, 2020 at 01:37 AM
Thanks to @tadadosi, I was able to get a script that should work for whatever setup you have. As they said you have to add depth to the mouse position to get the true worldposition, but it shouldn't be any arbitrary number or else it may not work as needed. Instead, use the absolute of your camera's z position
using UnityEngine;
public class Builder : MonoBehaviour
{
public GameObject go;
Camera cam;
public void Awake()
{
cam = Camera.main;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 worldPoint = Input.mousePosition;
worldPoint.z = Mathf.Abs(cam.transform.position.z);
//worldPoint.z = 11f;
Vector3 mouseWorldPosition = cam.ScreenToWorldPoint(worldPoint);
mouseWorldPosition.z = 0f;
Instantiate(go, mouseWorldPosition, Quaternion.identity);
}
}
}
Answer by tadadosi · Jun 19, 2020 at 12:55 PM
This is the best way I've found so far to transform from mouse screen pixel point to world point, the key here is to add depth to the pixel coordinates and then remove that depth after transforming to world coordinates.
Add this class to a gameobject, assign a prefab, hit play and (if you got an orthographic camera setup) you should be able to instantiate that prefab on the position where you pressed your left mouse button.
using UnityEngine;
public class InstantiateOnMousePosition : MonoBehaviour
{
public GameObject prefabToInstantiate;
private Camera _Camera;
private void Awake()
{
// Store your camera on Awake (better performance).
_Camera = Camera.main;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Store current mouse position in pixel coordinates.
Vector3 mousePixelPos = Input.mousePosition;
// Add depth so it can actually be used to cast a ray.
mousePixelPos.z = 20f;
// Transform from pixel to world coordinates
Vector3 mouseWorldPosition = _Camera.ScreenToWorldPoint(mousePixelPos);
// Remove depth
mouseWorldPosition.z = 0f;
// Spawn your prefab
Instantiate(prefabToInstantiate, mouseWorldPosition, Quaternion.identity);
}
}
}
@jmfp92 Where you able to find the answer to your question?
Thank you! that PRETTY $$anonymous$$UCH worked for me except I had to change change the mousePixelPos.z to be 10 not 20 to get the object to instantiate close to where I am clicking on the screen and I noticed the further along in any direction on the x or y axis I go from zero to instantiate it's less reliable (object instantiates further away from mouse position) I'm assu$$anonymous$$g there is probably some property of the camera that is a float value we can use to make this more reliable, any input on that?