Nerd For Tech
Published in

Nerd For Tech

Point & Click to Move in Unity

Now that we have our cutscenes in place, let’s start working on the playing aspect of our game. To start, we need a way to move our player around, and for this type of game we are going to be using a point and click style of movement.
First thing we will need to do is “bake” the floor so that we have a surface for our player and AI to walk on, and it prevents us from being able to click and move outside of the designated area. To back our floor, we will want to open the collider that has been made for this particular game:

Once we have baked our floor, we will notice that it turns blue. This blue highlight is the area that we are able to walk on, and it covers the whole location of this scene.
Now that we have our floor baked, let’s get our player setup so that we can start building our movement. Just for testing purposes, we will use a 3d cylinder as our player for now. Once we have it in place, we will want to add a nav mesh agent to our object so that it stays attached to our newly baked floor:

As we can see, if we have our object floating above the floor, the moment we start the game, it will snap to the floor.

Now we shall work towards getting our player moving upon a mouse click. To start, we will need to look into raycast, so let’s see what Unity says about it:

Now with a base to work with for our script, we can start to create the logic that our player object needs to move. First, we will create it so that we can see if we can get the location of our mouse click to register in the game:

With this code, we can see the locations that we click on, so let’s test that in our game:

Now that we have it showing where we click, let’s have a little fun with it and create a cube at the locations that we click to give an appearance of a path that our player will take:

Now that we can better visualize where we click, let’s get to work on creating actual movement for our player. Let’s look at the code we will write:

An addition we will need to make to our script is create a new namespace at the top of our script of “using UnityEngine.AI”. We put this in so that we can access the library of code used for this type of scripting we are about to use. From here, we will create a NavMeshAgent and tell it to grab the NavMeshAgent that we added onto our player. Finally we will go into the script portion that we just created and add in the SetDestination to have unity tell our player to move to the location that we click on.
Nest, we need to quickly adjust our objects to be static so that our player doesn’t just walk through them like they are holograms. To do this, we will just click on all of the objects that we have and click the static option in the inspector:

From here, we just go to the navigation, clear the current bake we have and create a new bake. With this done, our player will walk around the objects properly, which we can now test:

Now that we have created movement for our player, we can look towards the next step of our game.

--

--

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