I’m on Coherence 1.1.4, using rooms, this is on Unity 2023.2 at the moment. This behavior seems to be the same between Editor and WebGL.
I have fairly empty scene with a ball that is a sphere collider and rigid body, I have 2 player type characters. I have it setup so that when a trigger happens a force is applied to the ball.
I have things setup close to the physics example, when user A first logs into the room they have authority and the ball behaves and reacts as expected. When User B enters the room and tries to kick the ball it behaves erratically. I see authority bounce between the two and then stay with User A. I’m…not sure what I’m missing here.
Relevant Code:
This is the monobehavior on the ball:
private CoherenceSync _sync;
private Rigidbody _rigidbody;
private Collider _collider;
[Sync] public bool isBeingKicked = false;
private void Awake()
{
_collider = GetComponent<Collider>();
_rigidbody = GetComponent<Rigidbody>();
_sync = GetComponent<CoherenceSync>();
}
private void OnEnable()
{
_sync.OnAuthorityRequested += OnAuthorityRequested;
}
private void OnDisable()
{
_sync.OnAuthorityRequested -= OnAuthorityRequested;
}
private bool OnAuthorityRequested(ClientID requesterid, AuthorityType authoritytype, CoherenceSync sync)
{
return !isBeingKicked;
}
public void RequestAuthority(UnityAction callback)
{
if (_sync.HasStateAuthority)
{
callback();
}
else
{
isBeingKicked = true;
_sync.OnStateAuthority.AddListener(callback);
_sync.RequestAuthority(AuthorityType.Full);
}
}
public void RemoveListeners(UnityAction callback)
{
isBeingKicked = false;
_sync.OnStateAuthority.RemoveListener(callback);
}
And then this is on the player:
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Ball"))
{
if (other.TryGetComponent(out _ball))
{
_collidedRigidBody = other.GetComponent<Rigidbody>();
_ball.RequestAuthority(OnStateAuthority);
}
}
}
private void OnStateAuthority()
{
KickCollider();
_ball.RemoveListeners(OnStateAuthority);
_ball = null;
_collidedRigidBody = null;
}
private void KickCollider()
{
if (_collidedRigidBody != null)
{
var direction = (transform.position - _collidedRigidBody.transform.position).normalized;
_collidedRigidBody.AddForce(direction * multiplier, ForceMode.Impulse);
}
}
And this is the coherence sync settings I have on the ball prefab:
Any help to make this smooth would be appreciated.