Unity: Point-and-Click Movement

  • Requirement: Unity
  • Language: C#
  • Difficulty: Easy
  • Written by: Lee Zhi Eng
  • Last update: 14 Jul 2019

Introduction

In this tutorial, we will learn how to create a simple top down point-and-click movement in Unity. This type of movement is commonly used in RTS (Real-time Strategy games), RPG (Role-playing Games), Casual Farmer games, Adventure games and so on.

Setting Up Scene

1. Let’s start with an empty scene. First, add a plane, some cubes and a capsule to the scene. You can do so by going to Game Object ๐Ÿก’ 3D Object ๐Ÿก’ Cube/Capsule/Plane:

Create objects from top menu

The plane will serve as the ground, which you may up-scale it several times to sllow the player to have more space to walk around later. The cubes, on the other hand, are used as obstacles scattering around the ground plane. Finally, the capsule will be the player-controlled character who walks around the scene.

2. Right click on your project directory in Unity’s Project Panel and select Create
๐Ÿก’ Material to create a new material. Apply a grass image to the Albedo option and apply the material to the ground plane:

Unity material settings

We apply the grass texture to the ground plane so that we can easily see the movement of the player-controlled character compare to, say, walking a white plane. You may also want to adjust the tiling property to a higher value for better visual outcome.

Your scene should look something similar like this now:

Simple grass field scene

Next, we’ll learn how to generate navigation meshes for the scene, an essential step for navigation movement.

Baking Navigation Mesh

1. First, make sure that the ground plane and the obstacle cubes are marked as static before we proceed to the next step:

Check the static option

2. After that, go to Window ๐Ÿก’ AI ๐Ÿก’ Navigation and open up the Navigation panel.

3. Once the Navigation panel has been turned on, go to the Bake page and click the Bake button at the bottom.

4. After a few seconds, you should see a blue color navigation mesh appear on screen:

Navigation mesh fresh from the oven

You can play around with the Bake settings and see what different results it will produce. But for now, let’s stick to the default settings and move on to the next step.

Raycasting and Movement

1. Before we start writing any code, we must add a Nav Mesh Agent to the capsule character by selecting the capsule and go to Component ๐Ÿก’ Navigation ๐Ÿก’ Nav Mesh Agent. This component serves as the “agent” that helps to find the shortest path and moves the character to its destination.

2. After that, right click on the Project panel and select Create ๐Ÿก’ C# Script. Name the script as Player and apply it to the capsule by drag-and-drop the script onto the capsule.

3. Then, double click the script on the Project panel to open up Visual Studio. Once the Player script has been opened by Visual Studio, it should look something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }
}

4. Declare two public variables before Start(), namely agent and cam:

public class Player : MonoBehaviour
{
    public UnityEngine.AI.NavMeshAgent agent;
    public UnityEngine.Camera cam;

    // Start is called before the first frame update
    void Start()
    {
    }

The agent variable is the Nav Mesh Agent component which we added to the capsule previously in step 1, while the cam variable is the game camera, which we will talk about later. We will use these variables as the references to the components, which we can then modify its properties or call its functions.

5. After that, add the following code to the Update() function:

void Update()
{
    if (Input.GetMouseButton(0))
    {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            agent.SetDestination(hit.point);
        }
    }
}

In the code above, we check whether the left mouse button has been clicked. If it does, cast a ray from the position of the mouse cursor on screen toward the 3D scene. If the ray hits anything, Unity will return the hit point information, of which we can use to set the Nav Mesh Agent‘s destination.

6. Before we test run the game, drag and drop the capsule to the Agent property and the game camera to the Cam property on its Player script.

Errors will happen if these variables are not set

Try run the game now and click on random places on screen to move the capsule character around:

Move, move, move!

Great work, you have successfully implemented point-and-click movement in Unity!

Source Code

The full source code looks like the following:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public UnityEngine.AI.NavMeshAgent agent;
    public UnityEngine.Camera cam;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}

In the next tutorial, we will learn how to make a camera to smoothly following a game object.

Leave a Reply