Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Netcode;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.XR;
- using Unity.Netcode.Components;
- using UnityEngine.XR.Interaction.Toolkit;
- public class GripPoint : Interactable_Base
- {
- public enum GripType
- {
- ForeGrip,
- Handle,
- }
- public ConfigurableJoint joint;
- public HandController attachedController;
- public GripType Griptype;
- public Transform colliderParent;
- public Rigidbody rb;
- //public NetworkVariable<bool> isHeld;
- [ContextMenu("Add joint")]
- void AddJoints()
- {
- joint = rb.transform.AddComponent<ConfigurableJoint>();
- }
- private void Attach(HandController controller, bool resetposition = false)
- {
- joint.connectedBody = controller.rb;
- joint.autoConfigureConnectedAnchor = false;
- if (resetposition)
- {
- joint.anchor = Vector3.zero;
- joint.connectedAnchor = Vector3.zero;
- }
- else
- {
- joint.anchor = rb.transform.InverseTransformPoint(transform.position);
- //joint.connectedAnchor = rb.transform.InverseTransformPoint(transform.position);
- }
- joint.xMotion = ConfigurableJointMotion.Locked;
- joint.yMotion = ConfigurableJointMotion.Locked;
- joint.zMotion = ConfigurableJointMotion.Locked;
- joint.angularXMotion = ConfigurableJointMotion.Locked;
- joint.angularYMotion = ConfigurableJointMotion.Locked;
- joint.angularZMotion = ConfigurableJointMotion.Locked;
- }
- private void Detach(HandController controller)
- {
- joint.connectedBody = null;
- joint.autoConfigureConnectedAnchor = false;
- joint.xMotion = ConfigurableJointMotion.Free;
- joint.yMotion = ConfigurableJointMotion.Free;
- joint.zMotion = ConfigurableJointMotion.Free;
- joint.angularXMotion = ConfigurableJointMotion.Free;
- joint.angularYMotion = ConfigurableJointMotion.Free;
- joint.angularZMotion = ConfigurableJointMotion.Free;
- }
- [ServerRpc]
- public override void OnGrippedServerRpc(NetworkBehaviourReference Controller, ServerRpcParams serverRpcParams = default)
- {
- Debug.Log("interacted");
- HandController controller;
- Controller.TryGet(out controller, NetworkManager);
- if (isGripped)
- return;
- Collider[] colliderParents = colliderParent.GetComponentsInChildren<Collider>();
- foreach (Collider collider in controller.GetComponentsInChildren<Collider>())
- {
- foreach (Collider collider2 in colliderParents)
- {
- Physics.IgnoreCollision(collider, collider2, true);
- }
- }
- Attach(controller);
- }
- [ServerRpc]
- public override void UnGrippedServerRpc(NetworkBehaviourReference Controller, ServerRpcParams serverRpcParams = default)
- {
- HandController controller;
- Controller.TryGet(out controller, NetworkManager);
- Detach(controller);
- Collider[] colliderParents = colliderParent.GetComponentsInChildren<Collider>();
- foreach (Collider collider in controller.GetComponentsInChildren<Collider>())
- {
- foreach (Collider collider2 in colliderParents)
- {
- Physics.IgnoreCollision(collider, collider2, false);
- }
- }
- joint.connectedBody = null;
- joint.connectedAnchor = Vector3.zero;
- }
- [ServerRpc]
- public override void ButtonStartServerRpc(InputFeatureUsage<bool> inputFeature, ServerRpcParams serverRpcParams = default)
- {
- Debug.Log(inputFeature.name);
- base.ButtonStartServerRpc(inputFeature);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement