Files
BABA_YAGA/Assets/Scripts/Trap/BearTrap.cs
2026-07-02 08:59:23 +07:00

82 lines
2.9 KiB
C#

using System.Collections;
using Fusion;
using UnityEngine;
using Opsive.UltimateCharacterController.Traits;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Input;
public class BearTrap : TrapBase
{
protected override void OnTriggered(GameObject victim)
{
// 1. Server-side: Apply Damage to Opsive Health
ApplyDamage(victim);
// 2. Server-side: Send RPC to victim client to stun them
var netObj = victim.GetComponent<NetworkObject>();
if (netObj != null)
{
// Send RPC targeted to the victim client using targetPlayer
RPC_ApplyStun(netObj.InputAuthority, netObj.Id, trapData.EffectDuration);
}
// 3. Despawn the trap after a short delay (e.g. 2 seconds for animation/sfx to play)
StartCoroutine(DespawnAfterDelay(2f));
}
private void ApplyDamage(GameObject victim)
{
var health = victim.GetComponent<Health>();
if (health != null && health.IsAlive())
{
// Apply damage using Opsive UCC Health system
health.Damage(trapData.Damage, transform.position, Vector3.zero, 0);
Debug.Log($"[Server] BearTrap applied {trapData.Damage} damage to {victim.name}");
}
}
[Rpc(RpcSources.StateAuthority, RpcTargets.InputAuthority)]
private void RPC_ApplyStun([RpcTarget] PlayerRef targetPlayer, NetworkId victimId, float duration)
{
// Find the victim GameObject on the client using NetworkId
var victimNetObj = Runner.FindObject(victimId);
if (victimNetObj != null)
{
StartCoroutine(StunRoutine(victimNetObj.gameObject, duration));
}
}
private IEnumerator StunRoutine(GameObject target, float duration)
{
Debug.Log($"[Client] Applying root/stun to {target.name} for {duration} seconds");
// Disable UCC inputs and locomotion handling to freeze the player
var locomotionHandler = target.GetComponent<UltimateCharacterLocomotionHandler>();
var playerInput = target.GetComponent<Opsive.UltimateCharacterController.Input.PlayerInput>();
var locomotion = target.GetComponent<UltimateCharacterLocomotion>();
if (locomotionHandler != null) locomotionHandler.enabled = false;
if (playerInput != null) playerInput.enabled = false;
// Stop any current velocity
if (locomotion != null)
{
locomotion.ResetRotationPosition();
}
yield return new WaitForSeconds(duration);
// Re-enable UCC inputs and locomotion handling
if (locomotionHandler != null) locomotionHandler.enabled = true;
if (playerInput != null) playerInput.enabled = true;
Debug.Log($"[Client] Root/stun ended for {target.name}");
}
private IEnumerator DespawnAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
DespawnTrap();
}
}