Creating the “Point and Click” system to move the player in Unity

The exercise here demonstrates making a point and click movement system for the player.

The basic idea is pretty simple because maneuvering a space in a game shall be as logical as real space. There are objects on which one can not walk and there are areas which are meant to be walked on. Depending on the design of the game, both these aspects can be configured.

For now, it is kept pretty straightforward by only allowing movement on the floor plane.

So, below is a list of two items needed to make this system.

  • Navigation plane
  • A way to capture mouse input.
  1. Making the navigation plane

Here is the floor plane object that has a material attached to it. Then, reflection probes and lighting is doing its job on it.

Unity

If another plane for navigation has to be implemented, it must be put on this plane. But, that would erase the marble look here.

Solution?

Disable the mesh renderer of the navigation plane and place it at the floor level.

Unity
Unity

Now, the navigation plane needs to understand that it is a navigation plane.

“ The navigation system in Unity allows us to provide powerful path finding to the characters in our games very easily. ”

I recommend going through this Unity tutorial to understand this system.

After placing the navigation plane, select the navigation panel and bake the navmesh.

Unity
Unity

Once it gets baked, the important configuration to deploy is “navmeshagent” component on the player object.

The project at my hand has the player object in form of a capsule. Later on, the mesh renderer component was disabled and a 3d model was added as a child to this capsule.

Unity
Unity

This component works in conjunction with the navmesh plane. As soon as the game starts, the component would drop the object on the navmesh so that it looks like real walk and not floating.

  • Add a script to the player object and get hold of the navmesh component on this script.
    NavMeshAgent agent;
Vector3 target;

private void Start()
{
agent = GetComponent<NavMeshAgent>();
}
  • Using raycast, capture the mouse input.
void Update()
{
// Checking for a left-click

if (Input.GetMouseButtonDown(0))
{
// Cast a ray from mouse position

Ray myFirstRay = Camera.main.ScreenPointToRay(Input.mousePosition);

RaycastHit hitInfo;



if (Physics.Raycast(myFirstRay,out hitInfo)) //Bool returns true if there is collision.
{

agent.SetDestination(hitInfo.point);

target = hitInfo.point; // target is vector3 var.
}
}

That should be it.

Here is the final result :)

Thank you very much

Work in Progress.