Fixed
This commit is contained in:
@@ -5,14 +5,20 @@ using UnityEngine.Events;
|
||||
|
||||
public sealed class NetworkButtonInteractable : NetworkInteractable
|
||||
{
|
||||
[SerializeField] private UnityEvent pressed = new();
|
||||
[SerializeField] private UnityEvent<bool> buttonStateChanged;
|
||||
|
||||
private readonly NetworkVariable<bool> isPressed = new(
|
||||
false,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server);
|
||||
private readonly NetworkVariable<uint> pressSequence = new(
|
||||
0,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server);
|
||||
|
||||
public bool IsPressed => isPressed.Value;
|
||||
public uint PressSequence => pressSequence.Value;
|
||||
|
||||
public event Action Pressed;
|
||||
public event Action<bool> StateChanged;
|
||||
@@ -20,24 +26,35 @@ public sealed class NetworkButtonInteractable : NetworkInteractable
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
isPressed.OnValueChanged += HandleStateChanged;
|
||||
pressSequence.OnValueChanged += HandlePressSequenceChanged;
|
||||
ApplyState(isPressed.Value);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
isPressed.OnValueChanged -= HandleStateChanged;
|
||||
pressSequence.OnValueChanged -= HandlePressSequenceChanged;
|
||||
}
|
||||
|
||||
protected override bool CanInteractOnServer(ulong senderClientId) => true;
|
||||
|
||||
protected override void InteractOnServer(ulong senderClientId, NetworkObject playerObject)
|
||||
{
|
||||
Pressed?.Invoke();
|
||||
isPressed.Value = !isPressed.Value;
|
||||
pressSequence.Value++;
|
||||
}
|
||||
|
||||
private void HandleStateChanged(bool _, bool currentValue) => ApplyState(currentValue);
|
||||
|
||||
private void HandlePressSequenceChanged(uint previous, uint current)
|
||||
{
|
||||
if (current == previous)
|
||||
return;
|
||||
|
||||
Pressed?.Invoke();
|
||||
pressed?.Invoke();
|
||||
}
|
||||
|
||||
private void ApplyState(bool currentValue)
|
||||
{
|
||||
StateChanged?.Invoke(currentValue);
|
||||
|
||||
@@ -404,6 +404,12 @@ public sealed class NetworkDraggableInteractable : NetworkInteractable, IWorksta
|
||||
{
|
||||
bool shouldBeKinematic = IsExternallyControlled || !IsServer;
|
||||
|
||||
if (IsExternallyControlled && !physicsBody.isKinematic)
|
||||
{
|
||||
physicsBody.linearVelocity = Vector3.zero;
|
||||
physicsBody.angularVelocity = Vector3.zero;
|
||||
}
|
||||
|
||||
if (shouldBeKinematic)
|
||||
{
|
||||
physicsBody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
|
||||
@@ -417,11 +423,6 @@ public sealed class NetworkDraggableInteractable : NetworkInteractable, IWorksta
|
||||
physicsBody.collisionDetectionMode = ResolveDynamicCollisionDetectionMode();
|
||||
}
|
||||
|
||||
if (IsExternallyControlled)
|
||||
{
|
||||
physicsBody.linearVelocity = Vector3.zero;
|
||||
physicsBody.angularVelocity = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
if (HasAnyDragger)
|
||||
|
||||
@@ -37,8 +37,7 @@ public class NetworkInteractable : NetworkBehaviour, IInteractable
|
||||
if (!TryGetPlayerObject(senderClientId, out NetworkObject playerObject))
|
||||
return;
|
||||
|
||||
float maximumDistanceSquared = maximumInteractionDistance * maximumInteractionDistance;
|
||||
if ((playerObject.transform.position - transform.position).sqrMagnitude > maximumDistanceSquared)
|
||||
if (!IsWithinInteractionDistance(playerObject.transform.position))
|
||||
return;
|
||||
|
||||
if (!CanInteractOnServer(senderClientId))
|
||||
@@ -47,6 +46,28 @@ public class NetworkInteractable : NetworkBehaviour, IInteractable
|
||||
InteractOnServer(senderClientId, playerObject);
|
||||
}
|
||||
|
||||
private bool IsWithinInteractionDistance(Vector3 playerPosition)
|
||||
{
|
||||
float maximumDistanceSquared = maximumInteractionDistance * maximumInteractionDistance;
|
||||
if ((playerPosition - transform.position).sqrMagnitude <= maximumDistanceSquared)
|
||||
return true;
|
||||
|
||||
foreach (Collider interactionCollider in GetComponentsInChildren<Collider>(true))
|
||||
{
|
||||
if (interactionCollider == null || !interactionCollider.enabled ||
|
||||
!interactionCollider.gameObject.activeInHierarchy)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector3 closestPoint = interactionCollider.ClosestPoint(playerPosition);
|
||||
if ((playerPosition - closestPoint).sqrMagnitude <= maximumDistanceSquared)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected virtual bool CanInteractOnServer(ulong senderClientId) => false;
|
||||
protected virtual void InteractOnServer(ulong senderClientId, NetworkObject playerObject) { }
|
||||
|
||||
@@ -58,4 +79,3 @@ public class NetworkInteractable : NetworkBehaviour, IInteractable
|
||||
return playerObject != null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user