fix navmesh no find

cringe unity instantiate
C# + java cringe operator overloading making things not clea renough
This commit is contained in:
Crizomb 2025-01-28 00:08:00 +01:00
parent f8c859ed96
commit 769264c79b
4 changed files with 25 additions and 7 deletions

View file

@ -43,8 +43,7 @@ public class UnitButton : MonoBehaviour
void PlaceUnit()
{
Vector3 mousePos = unitPlacement.MapPosition();
GameObject go = Instantiate(unitPrefab);
go.transform.position = mousePos;
GameObject go = Instantiate(unitPrefab, mousePos, Quaternion.identity);
}

View file

@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.AI;
public class UnitPlacement : MonoBehaviour
{
@ -12,10 +13,24 @@ public class UnitPlacement : MonoBehaviour
mousePos.z = _camera.nearClipPlane;
Ray ray = _camera.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(ray,out hit, 100, placementLayer))
if (Physics.Raycast(ray, out hit, 100, placementLayer))
{
lastPosition = hit.point;
// Get the hit point from the raycast
Vector3 hitPoint = hit.point;
// Sample the closest valid position on the NavMesh
NavMeshHit navMeshHit;
if (NavMesh.SamplePosition(hitPoint, out navMeshHit, 1.0f, NavMesh.AllAreas))
{
lastPosition = navMeshHit.position; // Update lastPosition to the valid NavMesh position
}
else
{
Debug.LogWarning("No valid NavMesh position near the hit point.");
}
}
return lastPosition;
}
}
}