Unity Simple Player Movement – PointAndClickRPG

Posted by

Simple Player Movement on Navmesh

Decided to start off the dev with a point and click style RPG game. Going to use the Unity Game Engine because I’ve already had a go of it and know a little bit about how the UI works. I say a little bit because I have the memory of a goldfish and forget far quicker than I learn these days.

I’ve installed the latest version of the 2020.3 LTS and first of all I’m going to set up a simple prototyping scene. I’ll use a plane for the ground scaling it to x: 10 y: 1 z: 10, create a empty GameObject as the “Player” and shove a capsule under as the player graphics.

I’ll going to use Navmesh. Had to refresh my memory and one thing I will say is that the Unity documentation does seem really good. I’d never heard of the off-mesh link before. It sounds really cool will have to see if I can use it.

Firstly let’s get the player moving.

Here is the simple code from the video.

PlayerController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public NavMeshAgent Agent;

    private void Start()
    {
        Agent = GetComponent<NavMeshAgent>();
    }

    private void Update()
    {
        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            RaycastHit hit;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue()), out hit, 100))
            {
                Agent.destination = hit.point;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *