- Home /
How in the world do I ACCURATELY place an object at the mouse?
My game is a 2D topdown. The only collider on the scene is the on the player. The camera we're using is inside the player.
.
Using ScreenToWorldPoint becomes more inaccurate the farther away from the player the mouse is, and using RayCasts has given me absolutely nothing.
.
I've copied and pasted a million scripts and none of them work. Some place the object directly at (0,0), and some don't even place at all. Raycasts don't even know what to do because I obviously can't make my entire background a collider if players are moving through it.
.
This kinda works:
Vector3 mouseposition = Input.mousePosition;
mouseposition.z = 10;
Vector3 pos = (cam.ScreenToWorldPoint(mouseposition) - transform.position).normalized;
GameObject b = Instantiate(box , cam.ScreenToWorldPoint(mouseposition), Quaternion.identity);
But it "works" because it places it where your mouse is, but that is waaaay above the player. So set the Z to 0 immediately after placing so it's on the player's level? Well now it's inaccurate and doesn't place where you clicked at all!
Why can't you make the entire background a collider? Just give it its own layer and disable collisions between it and your game objects (player, mobs, missiles etc.).
And then use a mask to make your mouse raycast only register hits on your background collider. I've successfully used this method in a number of different games.
$$anonymous$$akes sense to me. Basically, because of layers, this is incorrect:.. "I obviously can't make my entire background a collider if players are moving through it."
"and disable collisions between it and your game objects" How? "And then use a mask" What is that, and also how?"
Answer by CatFisting · Dec 03, 2017 at 09:55 PM
It was easy as setting the camera from perspective to orthographic. This was far more infuriating than it should have been.
Answer by Propagant · Dec 03, 2017 at 10:30 AM
Hello, I think these topics could help you.
http://www.theappguruz.com/blog/pick-and-place-object-at-mouse-position-in-unity https://answers.unity.com/questions/574149/put-object-on-mouse-position.html http://coffeebreakcodes.com/move-object-to-mouse-click-position-unity3d/ http://saadkhawaja.com/move-3d-object-mouse-make-collide/
None of these work, and my script is extremely similar to a lot of those anyway. The reason I'm here is because I've already checked all of these. I don't need a bunch of links I've already seen, and tried, thanks.
Answer by villevli · Dec 03, 2017 at 04:03 PM
You want to set the z position relative to the camera so to place objects at the same z position as the player:
Vector3 mousePos = Input.mousePosition;
mousePos.z = -cam.transform.position.z + player.transform.position.z;
Vector3 worldPos = cam.ScreenToWorldPoint(mousePos);
Instantiate(box, worldPos, Quaternion.identity);
If the z position of the player is always 0 you don't need to add it.
Your answer
Follow this Question
Related Questions
Create objects on a mouse click in 2D 1 Answer
Object instantiates on top of the previous object 2 Answers
Instantiate gameobject at mouseposition 2d 2 Answers
Question about Instantiate 1 Answer