Unity point and click movement
Objective: Create a point and click move system using Unity AI navigation.
Create a 3D capsule and name it Player. Set the Player tag to player and add a Nav Mesh Agent component. Create a C# script called Player and add it to the Player game object. Use the following code to access the Nav Mesh Agent in void Start() of the Player script.
Open the Navigation by selecting Window > AI > Navigation from the Unity Menu. Select the FloorCollider game object which is a child of Objects.
The DetailedModel game object should be set to static so that the Nav Mesh Agent excludes environmental objects, such as display cases. Select Bake from the Bake tab of the Navigation window.
After baking, the scene should have a blue mesh as shown below.
Next add the following code, see below, which uses Physics.Raycast to detect where the mouse is located and move the player to the new location using the Nav Mesh Agent. First, check to see if the the left mouse button is clicked using Input.GetMouseButtonDown(0). Then create a vector or ray from the main camera to the mousePosition. Hit is used to store the collision. Notice a sphere is added so hit point is visible, for illustrative purposes only. Finally the Player is moved to hit.point using the _agent.destination.
The player can now move based on clicking the mouse on the screen.
The article uses assets from The Great Fleece from GameDevJon available on the Unity Asset Store.