Player Movement: Point & Click to Move in Unity

YShu
Cinematography-Stealth 3D Game
Sep 2, 2021

Goal: Creat the Point & Click function to control the movement of the player

  1. Create a Scripts folder in the Project tab to store all the upcoming scripts
  2. Create a Player.cs script and attach it to the Player gameobject
  3. Pseudocode for Updating player position:

// if left click
//cast a ray from mouse position (Raycasting: origin, and the hit position)
//debug the floor position hit

4. Move the player along the NavMesh

  • Pseudocode
    // Create a handle to nav mesh agent
    // In Start(), assign the handle to the NavMesh component
    // In Update(), handle.position = hitInfo.point
  • Code

1. Add a new name space to allow access to the NavMesh component

2. Create handle and assign NavMeshAgent

3. Set handle positionto hitInfo

Now, the player will move to the clicked position, but right now, it will go through the counters.

--

--