Point & Click to Move in Unity

Objective: Create a point and click movement using Unity’s AI Navigation

Before setting up the AI Navigation, I will set all the objects in the scene to static. This will leave a perimeter around the objects that will prevent the player from passing through the object.

Next, I will bake the NavMesh on the floor collider that is set up.

Once the NavMesh is baked, it will be shown by the blue shaded areas on the floor. Notice the clear areas around the object. This is because the Objects are set as static.

Now that I have the NavMesh in place, I can move on to the coding that will allow the player to move.

On the Player Object, I will have a script named Player. Open the script and add the Library for Unity AI.

Next, I will get a reference to the Nav Mesh Agent.

Now I will add the code to move the Player along the NavMesh.

The first thing I do is check if the left mouse button is clicked Input.GetMouseButtonDown(0).

Next, I will use Physics. Raycast to cast a ray to where the mouse is clicked. Ray rayOrigin = Camera.main.DcreenPointToRay(Input.MousePosition)

Then store the info in a variable hitInfo, RaycastHit hitInfo

Lastly, I will check for a hit and set the player's destination.

Now let’s see this in action.

Happy Coding!!!

--

--

--

Unity / C# Game developer

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

Recommended from Medium

AWS EC2 T2 Instances Demystified: Don’t Learn The Hard Way

Optimizing Your Spark Joins in an Unfashionable Manner

6 Ways to Find the Largest Number in a Python List

From LA Fashion Designer to NY Programmer

System Design: Typehead Suggestion

GSOC 2020: The Coding period commences!

10 Basic Features WordPress Should Come With

Using Amazon MSK with IAM Access Control for Apache Kafka and Node.js with KafkaJS

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
Bill Rislov

Bill Rislov

Unity / C# Game developer

More from Medium

Damage VFX using Animated Sprites in Unity

Tap & Hold & Rapid Prototyping w/ New Unity Input System

Bullet Pools in Unity

Code Optimization- Game Dev Series 145