# Authority transfer not consistent

**URL:** <https://community.coherence.io/t/authority-transfer-not-consistent/501>\
**Category:** Help\
**Created:** [April 22, 2024, 3:58pm UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501 "2024-04-22T15:58:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.picerno](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@michael.picerno](https://community.coherence.io/u/michael.picerno)\
**Post date:** [April 22, 2024, 3:58pm UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/1 "2024-04-22T15:58:36Z")

</div>

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:

```auto
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:

```auto
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:

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/coherence/original/1X/6e669bfd3293a74a62054acfb024195e4af6ae93.png)

Any help to make this smooth would be appreciated.

---

<div class="post-metadata">

**Author:** ![miguel-coherence](https://dub1.discourse-cdn.com/flex017/user_avatar/community.coherence.io/miguel-coherence/32/101_2.png) [@miguel-coherence](https://community.coherence.io/u/miguel-coherence)\
**Post date:** [April 24, 2024, 10:29am UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/2 "2024-04-24T10:29:07Z")

</div>

Hello Michael!

Looking at your code, I might have an idea whats happening, I’ll try to explain:

1. The OnTriggerEnter from the player doesn’t check if you have authority over the player. I feel like its important that this trigger only runs for authority players, that might be why authority keeps bouncing between them. Keep a reference to the CoherenceSync from your player and add a \_coherenceSync.HasStateAuthority to the check right next to the “Ball” tag comparison.

2. Another thing I see which might have undefined behaviour, is in the first script, in the RequestAuthority method, you are setting isBeingKicked to true when you DON’T have authority over the ball, this is not allowed and coherence will most likely override that value. Remember that even if you’re requesting authority, this is not instant, it takes time for this request to travel to the RS and be processed. isBeingKicked should only be set to true in the KickCollider method.

Hope this helps and thank you for trying coherence out

---

<div class="post-metadata">

**Author:** ![miguel-coherence](https://dub1.discourse-cdn.com/flex017/user_avatar/community.coherence.io/miguel-coherence/32/101_2.png) [@miguel-coherence](https://community.coherence.io/u/miguel-coherence)\
**Post date:** [April 24, 2024, 10:37am UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/3 "2024-04-24T10:37:31Z")

</div>

By the way, remember that when you don’t have authority over the ball, is important that the rigidbody is kinematic, so Unity physics don’t mess with coherence updates.

You can let coherence handle that automatically by going to the Configure window =\> Components tab and checking the RigidBody configuration there.

---

<div class="post-metadata">

**Author:** ![michael.picerno](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@michael.picerno](https://community.coherence.io/u/michael.picerno)\
**Post date:** [April 26, 2024, 6:19pm UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/4 "2024-04-26T18:19:06Z")

</div>

Setting kinematic seems to have cleaned up things quite a bit, thank you for the reply. The collision script on the player should only be on for the local player. I set that component to be disabled for any avatars instantiated by coherence. Unless you think I should have that check either way?

---

<div class="post-metadata">

**Author:** ![ciro](https://dub1.discourse-cdn.com/flex017/user_avatar/community.coherence.io/ciro/32/82_2.png) [@ciro](https://community.coherence.io/u/ciro)\
**Post date:** [May 1, 2024, 8:50am UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/5 "2024-05-01T08:50:25Z")

</div>

> [@michael.picerno](#):
>
> The collision script on the player should only be on for the local player. I set that component to be disabled for any avatars instantiated by coherence. Unless you think I should have that check either way?

Generally setting a component to be disabled protects you from undesired effects, but not with collision events. They will trigger anyway even if the component is disabled, so that is probably part of the picture of why you see so much glitching.

So yeah, either check for authority on the Sync, or check for the component’s enabled:

```auto
private void OnTriggerEnter(Collider other)
{
    if (enabled && other.CompareTag("Ball"))
    {
        // Do things
    }
}

```

**PS:** I also had to go and double-check whether the physics events were fired with a disabled component 😆 Apparently the Unity docs [used to mention it](https://docs.unity3d.com/2018.1/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html) specifically, but [the new page doesn’t](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html)! Come on, Unity!

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex017/uploads/coherence/original/1X/b3aa79eb9519fac7aad7eba18f15a1e8ce60f6e2.png) [@system](https://community.coherence.io/u/system)\
**Post date:** [July 30, 2024, 8:50am UTC](https://community.coherence.io/t/authority-transfer-not-consistent/501/6 "2024-07-30T08:50:43Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
