You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
public class VectorFencePrefab : NetworkBehaviour
|
|
{
|
|
[Header("Fence Settings")]
|
|
public VectorFenceAbility Ability;
|
|
private NavMeshObstacle navMeshObstacle;
|
|
[SerializeField] private List<NavMeshAgent> m_Agents = new List<NavMeshAgent>();
|
|
[SerializeField] private float searchRadius = 5f;
|
|
private void Awake()
|
|
{
|
|
navMeshObstacle = GetComponent<NavMeshObstacle>();
|
|
if (navMeshObstacle == null)
|
|
{
|
|
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<NavMeshAgent>();
|
|
if (agent && !m_Agents.Contains(agent)) m_Agents.Add(agent);
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
var agent = other.GetComponent<NavMeshAgent>();
|
|
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)
|
|
{
|
|
StartCoroutine(ScaleUp());
|
|
StartCoroutine(DestroyAfterLifetime());
|
|
}
|
|
}
|
|
|
|
private IEnumerator ScaleUp()
|
|
{
|
|
if (navMeshObstacle != null)
|
|
{
|
|
navMeshObstacle.enabled = true;
|
|
navMeshObstacle.carving = true;
|
|
navMeshObstacle.carveOnlyStationary = false;
|
|
navMeshObstacle.carvingMoveThreshold = 0.01f;
|
|
navMeshObstacle.center = Vector3.zero; // Adjust center if needed
|
|
navMeshObstacle.shape = NavMeshObstacleShape.Box;
|
|
}
|
|
float elapsedTime = 0f;
|
|
Vector3 originalScale = transform.localScale;
|
|
Vector3 targetScale = new Vector3(Ability.wallLength, Ability.wallHeight, Ability.wallWidth); // Target full height scale
|
|
|
|
while (elapsedTime < Ability.scaleUpDuration)
|
|
{
|
|
transform.localScale = Vector3.Lerp(originalScale, targetScale, elapsedTime / Ability.scaleUpDuration);
|
|
elapsedTime += Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
transform.localScale = targetScale;
|
|
|
|
|
|
}
|
|
|
|
private IEnumerator DestroyAfterLifetime()
|
|
{
|
|
yield return new WaitForSeconds(Ability.abilityDuration);
|
|
|
|
if (IsServer)
|
|
{
|
|
if (NetworkObject != null)
|
|
{
|
|
NetworkObject.Despawn(true);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
Debug.Log("[VectorFencePrefab] Fence destroyed.");
|
|
}
|
|
}
|
|
}
|