[csharp] GrabbableObject.cs

Viewer

copydownloadembedprintName: GrabbableObject.cs
  1. using System.Collections;
  2. using UnityEngine;
  3. using System.Linq;
  4. using Photon.Pun;
  5.  
  6. public abstract class GrabbableObject : MonoBehaviourPun
  7. {
  8.     private bool isGrabbed;
  9.     public bool IsGrabbed 
  10.     { 
  11.         private set
  12.         {
  13.             if (PhotonNetwork.IsMasterClient)
  14.             {
  15.                 photonView.RPC(nameof(SetGrabbedState), RpcTarget.OthersBufferedvalue);
  16.                 isGrabbed = value;
  17.             }
  18.             else
  19.             {
  20.                 photonView.RPC(nameof(SetGrabbedState), RpcTarget.AllBufferedvalue);
  21.             }
  22.  
  23.             EnableCollision(!isGrabbed);
  24.         }
  25.  
  26.         get => isGrabbed;
  27.     }
  28.  
  29.     private Collider[] colliders;
  30.     private Rigidbody[] rigidbodies;
  31.  
  32.     [SerializeField] private bool regainPhysics;
  33.  
  34.     // Start is called before the first frame update
  35.     private void Awake()
  36.     {
  37.         colliders = GetComponentsInChildren<Collider>().Append(GetComponent<Collider>()).ToArray();
  38.         rigidbodies = GetComponentsInChildren<Rigidbody>().Append(GetComponent<Rigidbody>()).ToArray();
  39.     }
  40.  
  41.     public virtual GrabbableObject Grab(Transform newParent)
  42.     {
  43.         if (IsGrabbed) return null;
  44.  
  45.         IsGrabbed = true;
  46.         transform.SetParent(newParent);
  47.         StartCoroutine(RotateTowardsParent(newParent));
  48.         return this;
  49.     }
  50.  
  51.     private IEnumerator RotateTowardsParent(Transform parent)
  52.     {
  53.         float time = 0;
  54.         float speed = 1f;
  55.  
  56.         Vector3 startPos = transform.position;
  57.         Quaternion startRotation = transform.rotation;
  58.  
  59.         while (time < 1f)
  60.         {
  61.             transform.position = Vector3.Lerp(startPos, parent.position, time);
  62.             transform.rotation = Quaternion.Slerp(startRotation, Quaternion.LookRotation(parent.forward), time);
  63.  
  64.             time += Time.fixedDeltaTime * speed;
  65.             yield return new WaitForFixedUpdate();
  66.         }
  67.     }
  68.  
  69.     public virtual GrabbableObject Throw()
  70.     {
  71.         IsGrabbed = false;
  72.         transform.SetParent(null);
  73.         return null;
  74.     }
  75.  
  76.     public abstract void Use(Vector3 direction);
  77.  
  78.     private void EnableCollision(bool enable)
  79.     {
  80.         foreach (var coll in colliders)
  81.         {
  82.             if (coll == null) continue;
  83.  
  84.             coll.enabled = enable;
  85.         }
  86.  
  87.         foreach (var rb in rigidbodies)
  88.         {
  89.             if (rb == null) continue;
  90.  
  91.             rb.isKinematic = !enable;
  92.         }
  93.     }
  94.  
  95.     [PunRPC]
  96.     private void SetGrabbedState(bool grabbed) => isGrabbed = grabbed;
  97. }
  98.  

Editor

You can edit this paste and save as new:


File Description
  • GrabbableObject.cs
  • Paste Code
  • 19 Sep-2021
  • 2.54 Kb
You can Share it: