Raycasting to a NavMesh in Unity

Objective: Get the position in the game world clicked by the user and position the player-character at that location.

Just like Nightcrawler from the X-Men!

We begin by defining a LayerMask to ignore any objects except for those on the Floor layer that are clicked by the player. When the player clicks, an invisible Ray is drawn from the main camera to the mouse position. If this ray hits an object on our floor layer, the RaycastHit info is passed into our hit variable. We can then use the hit.point to position our player!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store