forked from tmokmss/com.unity.multiplayer.samples.coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloorSwitch.cs
77 lines (62 loc) · 2.37 KB
/
FloorSwitch.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects
{
/// <summary>
/// Server-side logic for a floor switch (a/k/a "pressure plate").
/// This script should be attached to a physics trigger.
/// </summary>
[RequireComponent(typeof(Collider))]
public class FloorSwitch : NetworkBehaviour
{
[SerializeField]
Animator m_Animator;
[SerializeField]
Collider m_Collider;
public NetworkVariable<bool> IsSwitchedOn { get; } = new NetworkVariable<bool>();
List<Collider> m_RelevantCollidersInTrigger = new List<Collider>();
const string k_AnimatorPressedDownBoolVarName = "IsPressed";
[SerializeField, HideInInspector]
int m_AnimatorPressedDownBoolVarID;
void Awake()
{
m_Collider.isTrigger = true;
}
public override void OnNetworkSpawn()
{
if (!IsServer)
{
enabled = false;
}
FloorSwitchStateChanged(false, IsSwitchedOn.Value);
IsSwitchedOn.OnValueChanged += FloorSwitchStateChanged;
}
void OnTriggerEnter(Collider other)
{
// no need to check for layer; layer matrix has been configured to only allow FloorSwitch x PC interactions
m_RelevantCollidersInTrigger.Add(other);
}
void OnTriggerExit(Collider other)
{
m_RelevantCollidersInTrigger.Remove(other);
}
void FixedUpdate()
{
// it's possible that the Colliders in our trigger have been destroyed, while still inside our trigger.
// In this case, OnTriggerExit() won't get called for them! We can tell if a Collider was destroyed
// because its reference will become null. So here we remove any nulls and see if we're still active.
m_RelevantCollidersInTrigger.RemoveAll(col => col == null);
IsSwitchedOn.Value = m_RelevantCollidersInTrigger.Count > 0;
}
void FloorSwitchStateChanged(bool previousValue, bool newValue)
{
m_Animator.SetBool(m_AnimatorPressedDownBoolVarID, newValue);
}
void OnValidate()
{
m_AnimatorPressedDownBoolVarID = Animator.StringToHash(k_AnimatorPressedDownBoolVarName);
}
}
}