So this is my script for tile-based player movement with movement markers, any input or bad practices i should fix, please let me know! Thanks in advance
using UnityEngine;
using UnityEngine.AI;
public class PlayerActions : MonoBehaviour
{
bool playerIsSelected = false;
public LayerMask whatCanBeClicked;
public LayerMask whatCanBeMarked;
public GameObject marker;
GameObject PreviousMarker;
GameObject markerClone;
NavMeshAgent playerNav;
RaycastHit hitInfo;
Ray myRay;
void Update()
{
if (Input.GetMouseButtonDown(0)) //Player movment
{
myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out hitInfo, 100, whatCanBeClicked))
{
Debug.Log(hitInfo.collider);
if (hitInfo.collider.gameObject.CompareTag("Player"))
{
playerIsSelected = true;
playerNav = hitInfo.collider.GetComponent<NavMeshAgent>();
}
if (hitInfo.collider.gameObject.CompareTag("Ground") && playerIsSelected)
{
playerNav.SetDestination(hitInfo.collider.transform.position);
playerIsSelected = false;
}
}
}
if (playerIsSelected) // Movement marker
{
myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(myRay, out hitInfo, 100, whatCanBeMarked))
{
if (PreviousMarker == null)
{
Vector3 position = hitInfo.collider.gameObject.transform.position;
markerClone = Instantiate(marker, position, hitInfo.collider.gameObject.transform.rotation);
PreviousMarker = hitInfo.collider.gameObject;
}
if (PreviousMarker != hitInfo.collider.gameObject)
{
Destroy(markerClone);
PreviousMarker = null;
}
}
}
else
{
Destroy(markerClone);
PreviousMarker = null;
}
}
}