why cant i calculate the path? it only works if i use navmesh.allareas, which i don’t want to. otherwise it simply says it is an invalid path…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Entity : MonoBehaviour
{
public SpriteRenderer spriteRenderer;
private Rigidbody2D rb;
public Faction myFaction;
public float speed = 3f;
public Vector2 goToPosition = Vector2.zero;
private Vector2 direction;
private NavMeshPath myPath;
private int currentCornerIndex = 0;
public void InitializeEntity(Faction faction)
{
myFaction = faction;
spriteRenderer.color = myFaction.factionColor;
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
myPath = new NavMeshPath();
direction = Vector2.zero;
}
private void FixedUpdate()
{
Movement(goToPosition);
}
int customAreaMask = 0;
void SetupBitMaskForNavigation()
{
// Get area indices for Walkable, Jump, Not Walkable, and Water
int walkableArea = 1 << NavMesh.GetAreaFromName("Walkable");
int jumpArea = 1 << NavMesh.GetAreaFromName("Jump");
int notWalkableArea = 1 << NavMesh.GetAreaFromName("Not Walkable");
int waterArea = 1 << NavMesh.GetAreaFromName("Water");
// Combine Walkable and Jump areas using bitwise OR (|), and exclude Not Walkable and Water using bitwise AND NOT (& ~)
int customAreaMask = walkableArea | jumpArea; // Include Walkable and Jump areas
customAreaMask &= ~(notWalkableArea | waterArea);
}
void Movement(Vector2 destination)
{
// Re-Calculate the path
bool pathFound = NavMesh.CalculatePath(transform.position, destination, customAreaMask, myPath);
currentCornerIndex = 0;
if (!pathFound || myPath.corners == null || myPath.corners.Length == 0)
{
Debug.Log("Invalid path or no corners found!");
return;
}
if (Vector2.Distance((Vector2)transform.position, destination) <= 1)
{
Debug.Log("Arrived at destination");
rb.velocity = Vector2.zero;
return;
}
// Move towards the next corner in the path
Vector3 nextCorner = myPath.corners[currentCornerIndex];
if (Vector3.Distance(transform.position, nextCorner) < 0.1f)
{
currentCornerIndex++;
}
// Log and apply velocity
if (currentCornerIndex < myPath.corners.Length)
{
direction = ((Vector2)myPath.corners[currentCornerIndex] - (Vector2)transform.position).normalized;
rb.velocity = direction * speed;
}
else
{
rb.velocity = Vector2.zero;
}
}
private void OnDrawGizmosSelected()
{
if (myPath == null || myPath.corners == null || myPath.corners.Length == 0)
return;
// Set Gizmo color for the path
Gizmos.color = Color.green;
// Draw the path corners as spheres
for (int i = 0; i < myPath.corners.Length; i++)
{
Gizmos.DrawSphere(myPath.corners[i], 0.1f); // Draw a small sphere at each corner
// Draw lines between the corners
if (i > 0)
{
Gizmos.DrawLine(myPath.corners[i - 1], myPath.corners[i]);
}
}
}
float WanderTic;
float ChangeWanderDirectionTime = 3f;
float WanderRadius = 3f;
void Wander()
{
if(WanderTic > 0)
{
WanderTic -= Time.deltaTime;
}
else
{
WanderTic = ChangeWanderDirectionTime;
Vector3 randomDirection = Random.insideUnitSphere * WanderRadius;
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, WanderRadius, NavMesh.AllAreas);
Vector3 finalPosition = hit.position;
}
}
}