From 467e2eeecd7e03eff5d2739694fdca7323a628db Mon Sep 17 00:00:00 2001 From: Hazim Bin Ijaz Date: Fri, 10 Jan 2025 17:12:51 +0500 Subject: [PATCH] Update AbilitySystem.cs --- Assets/Scripts/Gameplay/AbilitySystem.cs | 77 +++++++++++++++++------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/Assets/Scripts/Gameplay/AbilitySystem.cs b/Assets/Scripts/Gameplay/AbilitySystem.cs index 02ef000..7fce81f 100644 --- a/Assets/Scripts/Gameplay/AbilitySystem.cs +++ b/Assets/Scripts/Gameplay/AbilitySystem.cs @@ -21,9 +21,11 @@ public class AbilitySystem : NetworkBehaviour [SerializeField] private Material invalidPlacementMaterial; [Header("Wall Placement Settings")] - [SerializeField] private float wallRotationSpeed = 1.2f; + [SerializeField] private float wallRotationSpeed = 0.5f; [SerializeField] private LayerMask playerLayer; // Layer for detecting players + [SerializeField] private float mouseMoveThreshold = 0.1f; // Minimum mouse movement threshold + private Vector3 initialMousePosition; private Vector3 wallSpawnPosition; private bool isWallPlacementStarted = false; private bool isValidPlacement = true; @@ -105,22 +107,9 @@ public class AbilitySystem : NetworkBehaviour if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit)) { wallIndicator.transform.position = hit.point; // Update position to follow the mouse + + isValidPlacement = IsPlacementValid(hit.point, wallIndicator.transform.rotation, playerLayer); - // Calculate half-extents based on wall's current scale - Vector3 halfExtents = new Vector3( - wallIndicator.transform.localScale.x / 2f, - wallIndicator.transform.localScale.y / 2f, - wallIndicator.transform.localScale.z / 2f - ); - - // Perform BoxCast aligned with the wall's orientation - isValidPlacement = !Physics.CheckBox( - hit.point, - halfExtents, - wallIndicator.transform.rotation, - playerLayer - ); - // Change indicator color based on placement validity var meshRenderer = wallIndicator.GetComponent(); if (meshRenderer != null) { @@ -136,6 +125,7 @@ public class AbilitySystem : NetworkBehaviour 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}"); } @@ -150,17 +140,60 @@ public class AbilitySystem : NetworkBehaviour { if (isWallPlacementStarted && wallIndicator != null) { - Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); - if (Physics.Raycast(ray, out RaycastHit hit)) + // Get the current mouse position in screen space + Vector3 currentMousePosition = Input.mousePosition; + + // Calculate the distance the mouse has moved since the initial click + float mouseDistance = Vector3.Distance(initialMousePosition, currentMousePosition); + + // Check if the movement exceeds the threshold + if (mouseDistance >= mouseMoveThreshold) { - Vector3 direction = (hit.point - wallIndicator.transform.position).normalized; - float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; - Quaternion targetRotation = Quaternion.Euler(0f, angle, 0f); - wallIndicator.transform.rotation = Quaternion.Lerp(wallIndicator.transform.rotation, targetRotation, Time.deltaTime * wallRotationSpeed); + Ray ray = Camera.main.ScreenPointToRay(currentMousePosition); + if (Physics.Raycast(ray, out RaycastHit hit)) + { + Vector3 direction = (hit.point - wallIndicator.transform.position).normalized; + float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; + Quaternion targetRotation = Quaternion.Euler(0f, angle, 0f); + + // Smooth rotation with Lerp + wallIndicator.transform.rotation = targetRotation; + // 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 + var meshRenderer = wallIndicator.GetComponent(); + if (meshRenderer != null) + { + meshRenderer.material = isValidPlacement ? validPlacementMaterial : invalidPlacementMaterial; + } + + if (!isValidPlacement) + { + Debug.Log("Cannot rotate wall here: Overlapping with another object."); + } + } } } } + + private bool IsPlacementValid(Vector3 position, Quaternion rotation, LayerMask layerMask) + { + + // Perform overlap check after applying rotation + Vector3 halfExtents = new Vector3( + wallIndicator.transform.localScale.x / 2f, + wallIndicator.transform.localScale.y / 2f, + wallIndicator.transform.localScale.z / 2f + ); + // Perform a CheckBox for the given parameters + bool isOverlap = Physics.CheckBox(position, halfExtents, rotation, layerMask); + + + return !isOverlap; // Return true if valid placement (no overlap) + } + private void ManageStandardAbilityIndicator() { if (currentAbilityIndicator != null)