11using UnityEngine ;
22using UnityEngine . InputSystem ;
3- using UnityEngine . Serialization ;
43
54public class GrabInput : MonoBehaviour
65{
76 public InputActionReference grabAction ;
87 public Move moveScript ;
98
109 private Grab _grabScript ;
11- private Grabbable _grabbableTarget ;
10+ private Grabbable _potentialTarget ;
11+ private Grabbable _grabbable ;
1212 private bool _canToss ;
1313
1414 private void Awake ( )
@@ -34,16 +34,16 @@ private void OnTriggerEnter(Collider other)
3434
3535 // Found a grabbable object
3636 if ( other . CompareTag ( "Grabbable" ) )
37- _grabbableTarget = other . gameObject . GetComponent < Grabbable > ( ) ;
37+ _potentialTarget = other . gameObject . GetComponent < Grabbable > ( ) ;
3838 }
3939
4040 private void OnTriggerExit ( Collider other )
4141 {
4242 if ( _grabScript . IsCarryingSomething )
4343 return ;
4444
45- if ( other . gameObject . GetComponent < Grabbable > ( ) == _grabbableTarget )
46- _grabbableTarget = null ;
45+ if ( other . gameObject . GetComponent < Grabbable > ( ) == _potentialTarget )
46+ _potentialTarget = null ;
4747 }
4848
4949 /// <summary>
@@ -53,44 +53,52 @@ private void OnGrabActionPerformed(InputAction.CallbackContext obj)
5353 {
5454 if ( ! _grabScript . IsCarryingSomething )
5555 {
56- if ( _grabbableTarget == null )
57- return ;
56+ // Pick up
57+ if ( _potentialTarget != null
58+ && ! _potentialTarget . isBeingCarried )
59+ {
60+ _canToss = false ;
5861
59- _canToss = false ;
60-
61- // Attempt to pick up Grabbable
62- _grabbableTarget . PickupValidated += OnPickUpValidated ;
63- _grabbableTarget . RequestPickup ( ) ; // This will fire an authority request if entity is remote
62+ // Pick up Grabbable
63+ _grabbable = _potentialTarget ;
64+ _grabbable . PickupValidated += OnPickUpValidated ;
65+ _grabbable . RequestPickup ( ) ; // This will fire an authority request if the entity is remote
66+ }
6467 }
6568 else if ( _canToss )
6669 {
6770 // Release or throw
6871 float speed = moveScript . ThrowSpeed ( ) ;
6972 _grabScript . Drop ( speed ) ;
7073 moveScript . ApplyThrowPushback ( speed ) ;
74+ _grabbable = null ;
7175 }
7276 }
7377
7478 /// <summary>
7579 /// Received a response from the grabbable we're trying to pick up.
76- /// Since the object could be remote, this includes a request of authority,
77- /// which might lead it to fail if the object is set to not concede authority.
80+ /// Since the object could be remote, this includes a request of authority.
7881 /// </summary>
7982 /// <param name="success">Whether the pickup was authorized or not.</param>
8083 private void OnPickUpValidated ( bool success )
8184 {
82- _grabbableTarget . PickupValidated -= OnPickUpValidated ;
85+ _grabbable . PickupValidated -= OnPickUpValidated ;
8386 _canToss = true ;
8487
8588 if ( success )
8689 {
87- PickUp ( ) ;
90+ // The object was just laying around
91+ _grabScript . PickUp ( _grabbable ) ;
92+ _potentialTarget = null ;
93+ }
94+ else
95+ {
96+ // Pickup can fail when a grabbable that was free up to a moment ago JUST
97+ // got picked up by another player on the network.
98+ // Locally this client is not aware yet because the Grabbable.isBeingCarried property
99+ // hasn't synced yet, but when requesting authority for the pickup - they get rejected
100+ // in the Grabbable.OnAuthorityRequested callback.
101+ _grabbable = null ;
88102 }
89- }
90-
91- private void PickUp ( )
92- {
93- _grabScript . PickUp ( _grabbableTarget ) ;
94- _grabbableTarget = null ;
95103 }
96104}
0 commit comments