Nov
13
2012

Place Objects on Space With Mouse

If you want to instantiate objects on empty space, you cannot use Raycasts (no colliders to hit).
But you can use ScreenToWorldPoint() with adjustable distance from camera.

(Or you could put invisible collider there..and if you need to adjust distance, move the collider..)

Webplayer:
http://unitycoder.com/upload/demos/PlaceObjectsOnSpace/

Download source:
instantiateAtSpace.unityPackage


2 Comments + Add Comment

  • I struggled with this a while back. Any way to prevent cubes from being build inside other cubes, without using an invisible collider out in front of the camera?

    The idea is a first person shooter, with some base building. So having a collider out in front seemed to basically act a shield to enemy fire.

    • some ideas to try,
      – If you use grid, then keep array of filled grid cells, so cannot place on them
      – Or, if no grid, keep list of objects and check if user tries to insert new object too close to existing (radius, check each building from list (basically find nearest and calculate distance)
      – Or have colliders/triggers, and then use layers to disable bullets from colliding on them

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.