Update AbilitySystem.cs

dev-hazim
Hazim Bin Ijaz 1 week ago
parent 49e9d4b035
commit 467e2eeecd

@ -21,9 +21,11 @@ public class AbilitySystem : NetworkBehaviour
[SerializeField] private Material invalidPlacementMaterial; [SerializeField] private Material invalidPlacementMaterial;
[Header("Wall Placement Settings")] [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 LayerMask playerLayer; // Layer for detecting players
[SerializeField] private float mouseMoveThreshold = 0.1f; // Minimum mouse movement threshold
private Vector3 initialMousePosition;
private Vector3 wallSpawnPosition; private Vector3 wallSpawnPosition;
private bool isWallPlacementStarted = false; private bool isWallPlacementStarted = false;
private bool isValidPlacement = true; private bool isValidPlacement = true;
@ -106,21 +108,8 @@ public class AbilitySystem : NetworkBehaviour
{ {
wallIndicator.transform.position = hit.point; // Update position to follow the mouse wallIndicator.transform.position = hit.point; // Update position to follow the mouse
// Calculate half-extents based on wall's current scale isValidPlacement = IsPlacementValid(hit.point, wallIndicator.transform.rotation, playerLayer);
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<MeshRenderer>(); var meshRenderer = wallIndicator.GetComponent<MeshRenderer>();
if (meshRenderer != null) if (meshRenderer != null)
{ {
@ -136,6 +125,7 @@ public class AbilitySystem : NetworkBehaviour
if (isValidPlacement) if (isValidPlacement)
{ {
wallSpawnPosition = hit.point; // Save spawn position wallSpawnPosition = hit.point; // Save spawn position
initialMousePosition = Input.mousePosition; // Store the initial mouse position on click
isWallPlacementStarted = true; isWallPlacementStarted = true;
Debug.Log($"[AbilitySystem] Wall placement started at {wallSpawnPosition}"); Debug.Log($"[AbilitySystem] Wall placement started at {wallSpawnPosition}");
} }
@ -150,17 +140,60 @@ public class AbilitySystem : NetworkBehaviour
{ {
if (isWallPlacementStarted && wallIndicator != null) if (isWallPlacementStarted && wallIndicator != null)
{ {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 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)
{
Ray ray = Camera.main.ScreenPointToRay(currentMousePosition);
if (Physics.Raycast(ray, out RaycastHit hit)) if (Physics.Raycast(ray, out RaycastHit hit))
{ {
Vector3 direction = (hit.point - wallIndicator.transform.position).normalized; Vector3 direction = (hit.point - wallIndicator.transform.position).normalized;
float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; float angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
Quaternion targetRotation = Quaternion.Euler(0f, angle, 0f); Quaternion targetRotation = Quaternion.Euler(0f, angle, 0f);
wallIndicator.transform.rotation = Quaternion.Lerp(wallIndicator.transform.rotation, targetRotation, Time.deltaTime * wallRotationSpeed);
// 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<MeshRenderer>();
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() private void ManageStandardAbilityIndicator()
{ {
if (currentAbilityIndicator != null) if (currentAbilityIndicator != null)

Loading…
Cancel
Save