I’m currently working on a small dungeon crawler which uses a point and click system for movement. I have some scripts setup for defining special targets (for things like chests, doors and enemies) and got the player navigating to them previously. However now it seems the player REFUSES to go to my Chest target script and I cannot work out why. Any help would be greatly appreciated ![]()
Here’s the player controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Dungeon.Base;
namespace Dungeon.Player
{
[AddComponentMenu("Dungeon/Player/Player Controller")]
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerController : MonoBehaviour
{
[Header ("Controller")]
public NavMeshAgent agent;
public Animator anim;
Vector3 current;
Vector3 previous;
Transform followTarget;
[Header ("Camera")]
public float sensitivity = 500;
[Space]
public float min = 15;
public float max = 60;
[Space]
public bool doInput;
public static PlayerController plr;
public Transform locationMarker;
public Transform pointer;
Vector3 targetLocation;
private void OnValidate()
{
if (agent == null)
{
agent = GetComponent<NavMeshAgent>();
}
}
private void Awake()
{
plr = this;
current = transform.position;
previous = transform.position;
}
public void Halt ()
{
followTarget = null;
agent.isStopped = true;
}
private void Update()
{
targetLocation = agent.destination;
current = transform.position;
if (followTarget != null)
{
agent.isStopped = false;
agent.SetDestination(followTarget.position);
print("Navigating to " + followTarget.name);
locationMarker.position = followTarget.position;
if (Vector3.Distance (transform.position, followTarget.position) < followTarget.GetComponent<SelectionTarget>().stopDistance)
{
Halt();
if (followTarget != null)
{
if (followTarget.GetComponent<SelectionTarget>())
{
if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Enemy)
{
//Attack Behaviour
}
else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Item)
{
//Grab Behaviour
anim.SetTrigger("interact");
followTarget.GetComponent<SelectionTarget>().OnInteract();
}
else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Door)
{
//Door Behaviour
anim.SetTrigger("interact");
followTarget.GetComponent<SelectionTarget>().OnInteract();
}
else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Switch)
{
//Switch Behaviour
anim.SetTrigger("interact");
followTarget.GetComponent<SelectionTarget>().OnInteract();
}
else if (followTarget.GetComponent<SelectionTarget>().GetSelection() == SelectType.Chest)
{
//Chest Behaviour
anim.SetTrigger("interact");
followTarget.GetComponent<SelectionTarget>().OnInteract();
}
}
}
followTarget = null;
}
}
if (doInput == true)
{
if (pointer != null)
{
RaycastHit p;
if (Physics.Raycast(CameraController.cam.ScreenPointToRay(Input.mousePosition), out p, 100f))
{
pointer.position = p.point;
}
}
if (Input.GetButtonDown("Fire2"))
{
Halt();
}
if (Input.GetButtonDown("Fire1"))
{
RaycastHit h;
if (Physics.Raycast(CameraController.cam.ScreenPointToRay(Input.mousePosition), out h, 100f))
{
agent.isStopped = false;
if (h.collider.GetComponent<SelectionTarget>())
{
print("SelectionTargeted");
followTarget = h.collider.transform;
}
else
{
locationMarker.position = h.point;
agent.SetDestination(h.point);
}
}
}
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
FindObjectOfType<CameraController>().ChangeOffset(1);
}
if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
FindObjectOfType<CameraController>().ChangeOffset(-1);
}
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.W))
{
FindObjectOfType<CameraController>().SetOffset(0);
FindObjectOfType<CameraController>().td = true;
}
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
FindObjectOfType<CameraController>().SetOffset(0);
FindObjectOfType<CameraController>().td = false;
}
CameraController.cam.fieldOfView += (-Input.GetAxisRaw("Mouse ScrollWheel") * Time.deltaTime) * sensitivity;
CameraController.cam.fieldOfView = Mathf.Clamp(CameraController.cam.fieldOfView, min, max);
}
if (current.magnitude - previous.magnitude != 0)
{
anim.SetBool("move", true);
locationMarker.gameObject.SetActive(true);
} else
{
anim.SetBool("move", false);
locationMarker.gameObject.SetActive(false);
}
previous = transform.position;
}
}
}