Advertisement
briannovius

Untitled

May 3rd, 2022
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Netcode;
  5. using Unity.VisualScripting;
  6. using UnityEngine;
  7. using UnityEngine.XR;
  8. using Unity.Netcode.Components;
  9. using UnityEngine.XR.Interaction.Toolkit;
  10.  
  11. public class GripPoint : Interactable_Base
  12. {
  13.     public enum GripType
  14.     {
  15.         ForeGrip,
  16.         Handle,
  17.        
  18.     }
  19.    
  20.     public ConfigurableJoint joint;
  21.  
  22.     public HandController attachedController;
  23.     public GripType Griptype;
  24.     public Transform colliderParent;
  25.     public Rigidbody rb;
  26.     //public NetworkVariable<bool> isHeld;
  27.  
  28.     [ContextMenu("Add joint")]
  29.     void AddJoints()
  30.     {
  31.         joint = rb.transform.AddComponent<ConfigurableJoint>();
  32.     }
  33.     private void Attach(HandController controller, bool resetposition = false)
  34.     {
  35.         joint.connectedBody = controller.rb;
  36.         joint.autoConfigureConnectedAnchor = false;
  37.  
  38.         if (resetposition)
  39.         {
  40.             joint.anchor = Vector3.zero;
  41.             joint.connectedAnchor = Vector3.zero;
  42.         }
  43.         else
  44.         {
  45.             joint.anchor = rb.transform.InverseTransformPoint(transform.position);
  46.             //joint.connectedAnchor = rb.transform.InverseTransformPoint(transform.position);
  47.         }
  48.  
  49.         joint.xMotion = ConfigurableJointMotion.Locked;
  50.         joint.yMotion = ConfigurableJointMotion.Locked;
  51.         joint.zMotion = ConfigurableJointMotion.Locked;
  52.         joint.angularXMotion = ConfigurableJointMotion.Locked;
  53.         joint.angularYMotion = ConfigurableJointMotion.Locked;
  54.         joint.angularZMotion = ConfigurableJointMotion.Locked;
  55.     }
  56.  
  57.     private void Detach(HandController controller)
  58.     {
  59.         joint.connectedBody = null;
  60.         joint.autoConfigureConnectedAnchor = false;
  61.        
  62.         joint.xMotion = ConfigurableJointMotion.Free;
  63.         joint.yMotion = ConfigurableJointMotion.Free;
  64.         joint.zMotion = ConfigurableJointMotion.Free;
  65.         joint.angularXMotion = ConfigurableJointMotion.Free;
  66.         joint.angularYMotion = ConfigurableJointMotion.Free;
  67.         joint.angularZMotion = ConfigurableJointMotion.Free;
  68.     }
  69.     [ServerRpc]
  70.     public override void OnGrippedServerRpc(NetworkBehaviourReference Controller, ServerRpcParams serverRpcParams = default)
  71.     {
  72.         Debug.Log("interacted");
  73.         HandController controller;
  74.         Controller.TryGet(out controller, NetworkManager);
  75.         if (isGripped)
  76.             return;
  77.        
  78.         Collider[] colliderParents = colliderParent.GetComponentsInChildren<Collider>();
  79.         foreach (Collider collider in controller.GetComponentsInChildren<Collider>())
  80.         {
  81.             foreach (Collider collider2 in colliderParents)
  82.             {
  83.                 Physics.IgnoreCollision(collider, collider2, true);
  84.             }      
  85.         }
  86.        
  87.         Attach(controller);
  88.     }
  89.     [ServerRpc]
  90.     public override void UnGrippedServerRpc(NetworkBehaviourReference Controller, ServerRpcParams serverRpcParams = default)
  91.     {
  92.         HandController controller;
  93.         Controller.TryGet(out controller, NetworkManager);
  94.        
  95.         Detach(controller);
  96.         Collider[] colliderParents = colliderParent.GetComponentsInChildren<Collider>();
  97.         foreach (Collider collider in controller.GetComponentsInChildren<Collider>())
  98.         {
  99.             foreach (Collider collider2 in colliderParents)
  100.             {
  101.                 Physics.IgnoreCollision(collider, collider2, false);
  102.             }
  103.         }
  104.         joint.connectedBody = null;
  105.         joint.connectedAnchor = Vector3.zero;
  106.     }
  107.     [ServerRpc]
  108.     public override void ButtonStartServerRpc(InputFeatureUsage<bool> inputFeature, ServerRpcParams serverRpcParams = default)
  109.     {
  110.         Debug.Log(inputFeature.name);
  111.         base.ButtonStartServerRpc(inputFeature);
  112.     }
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement