Why does instantiaitng by mouse click doesn’t instantiate on the plane or terrain, instead instantiate in the middle of empty space.
I am using this code:
if (Input.GetMouseButton (0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()) {
troop = Instantiate (inv.selectedTroop, Input.mousePosition, Quaternion.identity) as GameObject; }
Although, it instantiates two or three gameObjects at once. How can I solve this issue?
You’re going to need to add half your unit’s height to the position’s Y-axis, otherwise the mouse click’s Y position is going to be the exact level of the point where you clicked.
For example, if you create a flat plane, and then you create a capsule at the same Y level of the plane, the capsule will be half-way through the plane instead of on top. Whereas if your Y level is +1 of the plane’s Y level, the capsule will be on top because the capsule has a height of 2.
Edit: You can usually get your unit’s height from the collider you have attached to it.
I don’t think my question is well understood, the object doesn’t even instantiate over the plane. It instantiate in free space. Even when I press to instantiate over the plane!