Spaces:
Sleeping
Sleeping
File size: 1,544 Bytes
defab59 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using Fusion;
using UnityEngine;
public class DroppingObstacle : NetworkBehaviour
{
public float triggerZRange = 4f;
public float dropSpeed = 10f;
public float dropDistance = 3f;
public float resetDelay = 2.5f;
Vector3 startPos;
[Networked] public NetworkBool IsDropping { get; set; }
[Networked] public TickTimer ResetTimer { get; set; }
public override void Spawned()
{
startPos = transform.position;
}
public override void FixedUpdateNetwork()
{
if (!Object.HasStateAuthority)
return;
if (!IsDropping)
{
if (PlayerInRange())
IsDropping = true;
return;
}
Vector3 pos = transform.position;
pos.y -= dropSpeed * Runner.DeltaTime;
transform.position = pos;
if (pos.y <= startPos.y - dropDistance)
{
if (!ResetTimer.IsRunning)
ResetTimer = TickTimer.CreateFromSeconds(Runner, resetDelay);
}
if (ResetTimer.Expired(Runner))
{
transform.position = startPos;
IsDropping = false;
}
}
bool PlayerInRange()
{
DinoPlayer[] players = FindObjectsOfType<DinoPlayer>();
foreach (DinoPlayer p in players)
{
if (!p.IsAlive)
continue;
float dz = Mathf.Abs(p.transform.position.z - transform.position.z);
if (dz <= triggerZRange)
return true;
}
return false;
}
}
|