From 6fdbf52440f34367d34ab53c4627e3fbf25e2196 Mon Sep 17 00:00:00 2001 From: Ali Sharoz Date: Tue, 14 Jan 2025 16:27:09 +0500 Subject: [PATCH] Vector Wall Bug Fix code w.r.t wall --- Assets/Bouncy.physicMaterial | 14 ++++++ Assets/Bouncy.physicMaterial.meta | 8 +++ .../Abilities/VectorFencePrefab.prefab | 10 ++-- Assets/Scenes/BossRoom.unity | 8 +-- Assets/Scripts/Gameplay/AbilitySystem.cs | 50 +++++++++---------- Assets/Scripts/Gameplay/VectorFencePrefab.cs | 32 ++++++++++-- 6 files changed, 84 insertions(+), 38 deletions(-) create mode 100644 Assets/Bouncy.physicMaterial create mode 100644 Assets/Bouncy.physicMaterial.meta diff --git a/Assets/Bouncy.physicMaterial b/Assets/Bouncy.physicMaterial new file mode 100644 index 0000000..c7c23ba --- /dev/null +++ b/Assets/Bouncy.physicMaterial @@ -0,0 +1,14 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!134 &13400000 +PhysicMaterial: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Bouncy + dynamicFriction: 0 + staticFriction: 0 + bounciness: 1 + frictionCombine: 1 + bounceCombine: 3 diff --git a/Assets/Bouncy.physicMaterial.meta b/Assets/Bouncy.physicMaterial.meta new file mode 100644 index 0000000..d4d60b0 --- /dev/null +++ b/Assets/Bouncy.physicMaterial.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0d12993c8fabfeb4e971a0197446a3ba +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 13400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Abilities/VectorFencePrefab.prefab b/Assets/Prefabs/Abilities/VectorFencePrefab.prefab index b69c70a..f502bde 100644 --- a/Assets/Prefabs/Abilities/VectorFencePrefab.prefab +++ b/Assets/Prefabs/Abilities/VectorFencePrefab.prefab @@ -95,7 +95,7 @@ BoxCollider: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 9694722736535169} - m_Material: {fileID: 0} + m_Material: {fileID: 13400000, guid: 0d12993c8fabfeb4e971a0197446a3ba, type: 2} m_IncludeLayers: serializedVersion: 2 m_Bits: 0 @@ -103,11 +103,11 @@ BoxCollider: serializedVersion: 2 m_Bits: 0 m_LayerOverridePriority: 0 - m_IsTrigger: 0 + m_IsTrigger: 1 m_ProvidesContacts: 0 - m_Enabled: 0 + m_Enabled: 1 serializedVersion: 3 - m_Size: {x: 1, y: 1, z: 1} + m_Size: {x: 1.15, y: 1, z: 3.5} m_Center: {x: 0, y: 0, z: 0} --- !u!208 &8921202503349013827 NavMeshObstacle: @@ -137,7 +137,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Name: m_EditorClassIdentifier: - GlobalObjectIdHash: 3892629208 + GlobalObjectIdHash: 1554870052 InScenePlacedSourceGlobalObjectIdHash: 0 AlwaysReplicateAsRoot: 0 SynchronizeTransform: 1 diff --git a/Assets/Scenes/BossRoom.unity b/Assets/Scenes/BossRoom.unity index 32bc648..c2dd7f8 100644 --- a/Assets/Scenes/BossRoom.unity +++ b/Assets/Scenes/BossRoom.unity @@ -8896,7 +8896,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1676734515771252668, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.x - value: 0.39434478 + value: 0.3943448 objectReference: {fileID: 0} - target: {fileID: 1676734515771252668, guid: 0193228de87741d40a42e561901c9083, type: 3} propertyPath: m_LocalRotation.y @@ -9161,15 +9161,15 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 203267159508449512, guid: 36b3ee75677a1544191c0ddaaadd8140, type: 3} propertyPath: m_LocalRotation.x - value: 0.3943448 + value: 0.39434484 objectReference: {fileID: 0} - target: {fileID: 203267159508449512, guid: 36b3ee75677a1544191c0ddaaadd8140, type: 3} propertyPath: m_LocalRotation.y - value: 0.31044647 + value: 0.3104465 objectReference: {fileID: 0} - target: {fileID: 203267159508449512, guid: 36b3ee75677a1544191c0ddaaadd8140, type: 3} propertyPath: m_LocalRotation.z - value: -0.14352977 + value: -0.1435298 objectReference: {fileID: 0} - target: {fileID: 203267159508449512, guid: 36b3ee75677a1544191c0ddaaadd8140, type: 3} propertyPath: m_LocalEulerAnglesHint.x diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index 3b05c2e..ab53898 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -6,7 +6,7 @@ using Unity.BossRoom.Gameplay.GameplayObjects.Character; using Unity.Netcode; using UnityEngine; using UnityEngine.AI; -using static Codice.Client.Common.WebApi.WebApiEndpoints; +//using static Codice.Client.Common.WebApi.WebApiEndpoints; public class AbilitySystem : NetworkBehaviour { @@ -66,18 +66,18 @@ public class AbilitySystem : NetworkBehaviour if (Input.GetMouseButtonUp(0) && isWallPlacementStarted) { - if (isValidPlacement) + //if (isValidPlacement) { UseActiveAbility(); // Place the wall when LMB is released isWallPlacementStarted = false; } - else + /*else { UseActiveAbility(); // Place the wall when LMB is released isWallPlacementStarted = false; Invoke(nameof(RepositionAgent), 0.5f); Debug.Log("Invalid placement! Cannot place wall on top of another player."); - } + }*/ } } else @@ -121,11 +121,11 @@ public class AbilitySystem : NetworkBehaviour //AliSharoz isValidPlacement = IsPlacementValid(hit.point, wallIndicator.transform.rotation, playerLayer); - //var meshRenderer = wallIndicator.GetComponent(); - //if (meshRenderer != null) - //{ - // meshRenderer.material = isValidPlacement ? validPlacementMaterial : invalidPlacementMaterial; - //} + /*var meshRenderer = wallIndicator.GetComponent(); + if (meshRenderer != null) + { + meshRenderer.material = isValidPlacement ? validPlacementMaterial : invalidPlacementMaterial; + }*/ } } @@ -133,27 +133,25 @@ public class AbilitySystem : NetworkBehaviour { if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { - if (isValidPlacement) + //if (isValidPlacement) { wallSpawnPosition = hit.point; // Save spawn position initialMousePosition = Input.mousePosition; // Store the initial mouse position on click isWallPlacementStarted = true; Debug.Log($"[AbilitySystem] Wall placement started at {wallSpawnPosition}"); } - else + /*else { wallSpawnPosition = hit.point; // Save spawn position initialMousePosition = Input.mousePosition; // Store the initial mouse position on click isWallPlacementStarted = true; Debug.Log("Cannot place the wall on top of another player."); - Invoke(nameof(RepositionAgent), 0.5f); - - } + }*/ } } - public float searchRadius = 5f; - - private void RepositionAgent() + /*public float searchRadius = 5f; + + private void RepositionAgent() { Debug.Log("RepositionAgent()1"); //if (NavMesh.SamplePosition(currentPosition, out hit, 0.01f, NavMesh.AllAreas)) @@ -180,7 +178,7 @@ public class AbilitySystem : NetworkBehaviour //{ // Debug.LogWarning("No valid NavMesh point found near the agent!"); //} - } + }*/ private void RotateWallIndicator() { if (isWallPlacementStarted && wallIndicator != null) @@ -206,20 +204,20 @@ public class AbilitySystem : NetworkBehaviour wallIndicator.transform.rotation = Quaternion.Lerp(wallIndicator.transform.rotation, targetRotation, Time.deltaTime * wallRotationSpeed); isValidPlacement = IsPlacementValid(wallIndicator.transform.position, wallIndicator.transform.rotation, playerLayer); + /* // Change indicator color based on placement validity - //AliSharoz - //var meshRenderer = wallIndicator.GetComponent(); - //if (meshRenderer != null) - //{ - // meshRenderer.material = isValidPlacement ? validPlacementMaterial : invalidPlacementMaterial; - //} + AliSharoz + var meshRenderer = wallIndicator.GetComponent(); + if (meshRenderer != null) + { + meshRenderer.material = isValidPlacement ? validPlacementMaterial : invalidPlacementMaterial; + } if (!isValidPlacement) { Debug.Log("Cannot rotate wall here: Overlapping with another object."); Invoke(nameof(RepositionAgent), 0.5f); - - } + }*/ } } } diff --git a/Assets/Scripts/Gameplay/VectorFencePrefab.cs b/Assets/Scripts/Gameplay/VectorFencePrefab.cs index 0d4386a..59bdd61 100644 --- a/Assets/Scripts/Gameplay/VectorFencePrefab.cs +++ b/Assets/Scripts/Gameplay/VectorFencePrefab.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Generic; using Unity.Netcode; using UnityEngine; using UnityEngine.AI; @@ -8,7 +9,8 @@ public class VectorFencePrefab : NetworkBehaviour [Header("Fence Settings")] public VectorFenceAbility Ability; private NavMeshObstacle navMeshObstacle; - + [SerializeField] private List m_Agents = new List(); + [SerializeField] private float searchRadius = 5f; private void Awake() { navMeshObstacle = GetComponent(); @@ -16,10 +18,34 @@ public class VectorFencePrefab : NetworkBehaviour { Debug.LogError("[VectorFencePrefab] NavMeshObstacle is missing on the prefab!"); } - transform.localScale = new Vector3(Ability.wallLength, 0, Ability.wallWidth); // Start with zero height } + + private void Start() => Invoke(nameof(RepositionAgents), 0.5f); + + private void OnTriggerEnter(Collider other) + { + var agent = other.GetComponent(); + if (agent && !m_Agents.Contains(agent)) m_Agents.Add(agent); + } + + private void OnTriggerExit(Collider other) + { + var agent = other.GetComponent(); + if (agent) m_Agents.Remove(agent); + } + + private void RepositionAgents() + { + m_Agents.RemoveAll(agent => agent == null); + m_Agents.ForEach(agent => + { + if (NavMesh.SamplePosition(agent.transform.position, out NavMeshHit hit, searchRadius, NavMesh.AllAreas)) + agent.Warp(hit.position); + }); + } + public override void OnNetworkSpawn() { if (IsServer) @@ -53,7 +79,7 @@ public class VectorFencePrefab : NetworkBehaviour transform.localScale = targetScale; - + } private IEnumerator DestroyAfterLifetime()