public void DetachIfAny() { if (attached) { attached.OnDetach(this); attached = null; SendHapticImpulse(hapticSettings.detachStrength, hapticSettings.detachDuration); } attached = null; }
public void TransforAttached(XRHand other) { if (attached) { other.DetachIfAny(); other.attached = attached; foreach (var c in attached.GetComponentsInChildren <Collider>()) { other.IgnoreCollider(c); } attached = null; SendHapticImpulse(hapticSettings.detachStrength, hapticSettings.detachDuration); other.SendHapticImpulse(other.hapticSettings.attachStrength, other.hapticSettings.attachDuration); } }
public void Attach(XRInteractable interactable, Vector3 handAttachPositionWS, Quaternion handAttachRotationWS) { if (!interactable.CanAttach(this, out _)) { return; } DetachIfAny(); attached = interactable; foreach (var c in attached.GetComponentsInChildren <Collider>()) { IgnoreCollider(c); } interactable.OnAttach(this, handAttachPositionWS, handAttachRotationWS); SendHapticImpulse(hapticSettings.attachStrength, hapticSettings.attachDuration); }
public bool isHovering(XRInteractable interactable) { int grabHitNum = Physics.SphereCastNonAlloc(grabSphere.transform.position - grabSphere.radius * grabSphere.transform.localScale.x * grabSphere.transform.forward, grabSphere.radius * grabSphere.transform.localScale.x, grabSphere.transform.forward, _hits, (grabSphere.center.z + grabSphere.radius) * grabSphere.transform.localScale.x, interactionLayer); for (int i = 0; i < grabHitNum; ++i) { var hit = _hits[i]; var it = hit.collider.attachedRigidbody ? hit.collider.attachedRigidbody.GetComponent <XRInteractable>() : hit.collider.GetComponent <XRInteractable>(); if (it == interactable) { return(true); } } return(false); }
void UpdateHovering() { XRInteractable newHovering = null; if (isEmpty) { /*int grabHitNum = Physics.SphereCastNonAlloc(grabSphere.transform.position - grabSphere.radius * grabSphere.transform.localScale.x * grabSphere.transform.forward, * grabSphere.radius * grabSphere.transform.localScale.x, * grabSphere.transform.forward, * _hits, * (grabSphere.center.z + grabSphere.radius) * grabSphere.transform.localScale.x, * interactionLayer, * QueryTriggerInteraction.Collide);*/ /*PhysicsEX.SphereCast(grabSphere.transform.position - grabSphere.radius * grabSphere.transform.localScale.x * grabSphere.transform.forward, * grabSphere.radius * grabSphere.transform.localScale.x, * grabSphere.transform.forward, * out RaycastHit junk, * (grabSphere.center.z + grabSphere.radius) * grabSphere.transform.localScale.x, * interactionLayer);*/ int grabHitNum = Physics.OverlapSphereNonAlloc(grabSphere.transform.TransformPoint(grabSphere.center), grabSphere.radius * grabSphere.transform.localScale.x, colliderBuffer, interactionLayer, QueryTriggerInteraction.Collide); int maxP = int.MinValue; float minD = float.MaxValue; for (int i = 0; i < grabHitNum; ++i) { /* * var hit = _hits[i]; * if (hit.distance==0 || Vector3.Dot(grabSphere.transform.forward, hit.point - grabSphere.transform.position) < 0) continue; * var it = hit.collider.attachedRigidbody ? hit.collider.attachedRigidbody.GetComponent<XRInteractable>() : hit.collider.GetComponent<XRInteractable>();*/ var cld = colliderBuffer[i]; var it = cld.attachedRigidbody ? cld.attachedRigidbody.GetComponent <XRInteractable>() : cld.GetComponent <XRInteractable>(); if (!it) { continue; } Vector3 clostestPoint = cld.ClosestPoint(grabSphere.transform.position); if (Vector3.Dot(grabSphere.transform.forward, clostestPoint - grabSphere.transform.position) < 0) { continue; } float distance = Vector3.Distance(clostestPoint, grabSphere.transform.position); if (!it.isActiveAndEnabled) { continue; } if (it.CanInteract(this, out int p)) { if (p > maxP || p == maxP && distance < minD) { maxP = p; minD = distance; newHovering = it; hoveringHitPosition = clostestPoint; hoveringHitRotation = rotation; /* * float cutoff = grabSphere.radius * grabSphere.transform.lossyScale.x*.5f; * if (hit.distance < cutoff)//TODO better judgement? * { * hoveringHitPosition = position; * hoveringHitRotation = rotation; * } * else * { * hoveringHitPosition = position+ grabSphere.transform.forward*(hit.distance- cutoff); * hoveringHitRotation = rotation;//TODO adjust it by hit.normal? * } */ } } } if (!newHovering) { if (Physics.SphereCast(grabSphere.transform.TransformPoint(grabSphere.center) + transform.forward * grabSphere.radius * grabSphere.transform.localScale.x * .0f, grabSphere.radius * grabSphere.transform.localScale.x, transform.forward, out RaycastHit hit2, playerRoot.lossyScale.x * pickUpDistTS, interactionLayer, QueryTriggerInteraction.Collide)) { var it = hit2.collider.attachedRigidbody ? hit2.collider.attachedRigidbody.GetComponent <XRInteractable>() : hit2.collider.GetComponent <XRInteractable>(); if (it && it.isActiveAndEnabled && it.CanInteract(this, out _)) { newHovering = it; hoveringHitPosition = hit2.point; hoveringHitRotation = rotation; } } } } if (hovering != newHovering && hovering != null && hovering != otherHand.hovering) { hovering.SetHovering(false); } if (newHovering) { newHovering.SetHovering(true); } if (hovering != newHovering) { if (newHovering == null) { SendHapticImpulse(hapticSettings.hoveringExitStrength, hapticSettings.hoveringEnterDuration); } else { SendHapticImpulse(hapticSettings.hoveringEnterStrength, hapticSettings.hoveringEnterDuration); } } hovering = newHovering; }