Spawn objects at click position in Unity

Daniel Kirwan
2 min readJun 15, 2021

Objective: Show how spawn primitive objects in game when you click the left mouse button

Above is the end result that will be accomplished. This is all in aid of getting a point and click movement system in place. First I need to know where the player is clicking inside the game world.

But before I do that I should let you know that I am using Unity’s Navmesh system for my point and click movement, now that is out the way let me show you how to spawn a primitive shape in your game world.

I am using a very simple script that uses raycasting. I first get the position of the mouse using the Ray and setting it equal to the main cameras point, using the mouse position.

I then set up the raycast object.

Next is calling the raycast and if there is a hit, I am debugging the position of the click in world coordinates. Then I am creating a primitive gameobject of a cube and setting the position to be the same as the mouse click.

And that is it for this article. The next one will be about adding in the player movement that follows the mouse clicks of the player. Come back soon.

--

--