Point & Click to Move in Unity

Objective: Set up logic for point and click character controller

While prototyping the Players gameplay we will use a 3D capsule object called Player in place of the player. Set up a scripts folder in the project, create a script called Player and drag it onto the Player(Capsule) in the hierarchy.

We will use a baked navmesh to identify to areas in the scene that our Player can move. You can see this navmesh in the above image, represented bty the light blue color around the level. For the Player to Identify and Interact with the navmesh, a Navmesh Agent is added to the Player.

We will be using raycasts and keyboard/mouse buttons for this controller. For more info on Raycasting in Unity visit Unity Docs.

For the initial action in getting the point and click system working, we need to allow the mouse to click an are in the scene and get/return the co-ordinates of that position. For this we need to check for the left mouse button click, casting a ray from the mouse position in the scene then getting the position in the scene back if we hit something as co-ordinates, which we can demo by creating primitive shapes such as cubes in Unity.

Next we need to move the Player to the point that has been clicked in the scene. The Player needs to move along the navmesh. Unity Doc.

We need to use the Unity AI namespace so we can import that first. We can then create a Private NavMeshAgent(called _agent) and assign it in the Start method using GetComponent. Using the UnityEngine.AI and NavMeshAgent gives us access to the SetDestination method, which we use for our newly assigned _agent to set the destination of the hitInfo point.

--

--

--

Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Rockabilly Instrumental Music Played Its Part In Rock And Roll History Too

Music Sparks Emotion in a Video Commercial

I can’t believe I actually miss 𝙥𝙚𝙤𝙥𝙡𝙚.

How is a pandemic like freelancing?

Quit the Twitters? You think I haven’t tried?

I need a hobby.

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
Simon Leen

Simon Leen

Developer

More from Medium

Modular Stealth Enemy AI in Unity

Post Processing Primer for Unity

I think you dropped something — Creating a simple loot drop system in Unity2D

Cinematography In Unity — Cameras In Cinemachine