- Home /
Is it possible to show the path the navmesh agent is taking in game?
Hey there, I'm making a click to move game where the player has to avoid enemies and get across the map. Sometimes it can be quite difficult because you can't tell where the player is going to go ( takes the fastest route ) so I was wondering if it's possible to show where it's going? I think id have to use a line renderer but I have no idea how to do that. Heres the code I've used for the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (PlayerController))]
public class Player : LivingEntity {
Camera cam;
PlayerController controller;
protected override void Start () {
base.Start();
cam = Camera.main;
controller = GetComponent<PlayerController>();
}
void Update () {
Ray rray = cam.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayDistance;
if (groundPlane.Raycast(rray, out rayDistance))
{
Vector3 point = rray.GetPoint(rayDistance);
//////////////////////////////////////////
Debug.DrawLine(rray.origin, point, Color.red);
controller.LookAt(point);
}
}
and here's the code for the player controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerController : MonoBehaviour {
// CAMERA
public Camera cam;
// NAVMESH AGENT ATTACHED TO PLAYER
public NavMeshAgent agent;
// Update is called once per frame
void Update ()
{
// IF LEFT MOUSE BUTTON IS PRESSED THEN PLAYER WILL MOVE TO WHERE YOU CLICKED
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
// MOVE THE AGENT
agent.SetDestination(hit.point);
}
}
}
public void LookAt (Vector3 lookPoint)
{
Vector3 heightCorrectedPoint = new Vector3(lookPoint.x, transform.position.y, lookPoint.z);
transform.LookAt(heightCorrectedPoint);
}
}
Would the line renderer go from the player to the hit.point? and if so how do I do that? I would like to show the exact path the player is going to take in order to get to where I clicked and then disappear once it gets to that point. Similar to how it's done in the xcom games. Any help is appreciated.
(Hope what I'm trying to do makes sense)
Thanks.
Your answer
Follow this Question
Related Questions
Nav Mesh Agent makes object disappear!!! Help!!,Nav Mesh Agent makes object disappear!! Help 2 Answers
NavMesh Agent Obstacle Avoidance Ignore 1 Answer
How to add variables to Components (Nav Mesh Agent)? 0 Answers
Modify NavMeshPath for Tactical FPS (slicing the pie/sweeping maneuver) 0 Answers
NavMesh Agent Controlled AI Sliding Toward next waypoint 0 Answers