From 769264c79bef5614fec32c775258e06dc3989f1c Mon Sep 17 00:00:00 2001 From: Crizomb Date: Tue, 28 Jan 2025 00:08:00 +0100 Subject: [PATCH] fix navmesh no find cringe unity instantiate C# + java cringe operator overloading making things not clea renough --- Assets/Scenes/LevelsTest/Flat 2.unity | 4 ++-- Assets/Scenes/LevelsTest/Pumpkin.unity | 4 ++++ Assets/Scripts/UnitButton.cs | 3 +-- Assets/Scripts/UnitPlacement.cs | 21 ++++++++++++++++++--- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Assets/Scenes/LevelsTest/Flat 2.unity b/Assets/Scenes/LevelsTest/Flat 2.unity index 9607612..9d207b5 100644 --- a/Assets/Scenes/LevelsTest/Flat 2.unity +++ b/Assets/Scenes/LevelsTest/Flat 2.unity @@ -3924,7 +3924,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 6208992634893184319, guid: e2eadb5c4fdf45ea995b4da7c9110275, type: 3} propertyPath: m_LocalPosition.y - value: -0.091646194 + value: 0 objectReference: {fileID: 0} - target: {fileID: 6208992634893184319, guid: e2eadb5c4fdf45ea995b4da7c9110275, type: 3} propertyPath: m_LocalPosition.z @@ -29482,7 +29482,7 @@ Transform: m_GameObject: {fileID: 2002548185} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -46.1, y: 4.1, z: 56.2} + m_LocalPosition: {x: -46.1, y: 2.81, z: 56.2} m_LocalScale: {x: 2.3030813, y: 2.3030813, z: 2.3030813} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Assets/Scenes/LevelsTest/Pumpkin.unity b/Assets/Scenes/LevelsTest/Pumpkin.unity index 4fe3ae4..9c4afb8 100644 --- a/Assets/Scenes/LevelsTest/Pumpkin.unity +++ b/Assets/Scenes/LevelsTest/Pumpkin.unity @@ -837,6 +837,10 @@ PrefabInstance: propertyPath: m_Name value: ZombieA objectReference: {fileID: 0} + - target: {fileID: 4976282506935113607, guid: a0750dd4706e5480da2ad597b62d8531, type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} - target: {fileID: 5387376515481739062, guid: a0750dd4706e5480da2ad597b62d8531, type: 3} propertyPath: m_AnchorMax.x value: 0 diff --git a/Assets/Scripts/UnitButton.cs b/Assets/Scripts/UnitButton.cs index 5108162..9f454d4 100644 --- a/Assets/Scripts/UnitButton.cs +++ b/Assets/Scripts/UnitButton.cs @@ -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); } diff --git a/Assets/Scripts/UnitPlacement.cs b/Assets/Scripts/UnitPlacement.cs index 03056c6..cdca0c7 100644 --- a/Assets/Scripts/UnitPlacement.cs +++ b/Assets/Scripts/UnitPlacement.cs @@ -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; } -} +} \ No newline at end of file