/// <summary> /// Positions the plug along the socket axis until the unsnap conditions are met or the plug is let go by the user /// </summary> /// <returns></returns> private IEnumerator SnapToSocket() { yield return(null); LockedPlug.PlugTransform.SetParent(null); rotationSnap.SnapToTarget(PlugConnectionPoint.rotation, RotationSnapSpeed); bool aligned = IsPlugOnSocketAxis(); Vector3 closestAllowedStartPosition = PlugConnectionPoint.position - PlugConnectionPoint.forward * MinInitialSocketDistance; while (LockedPlug != null) { if (!aligned) { PositionPlugOnJackAxis(closestAllowedStartPosition); if (positionSnap.HasReachedTarget) { aligned = true; } } else { PositionPlugOnJackAxis(PlugConnectionPoint.position); } yield return(new WaitForEndOfFrame()); } yield return(null); }
/// <summary> /// Quickly moves the plug back to the controller and sets it back to its initial state /// </summary> /// <returns></returns> private IEnumerator SnapModelToOrigin() { snappingEnabled = false; positionSnap.SnapToTarget(transform.position, SnapToHandSpeed); float originalCloseEnough = positionSnap.closeEnoughDistance; positionSnap.closeEnoughDistance = 0.015f; rotationSnap.SnapToTarget(transform.rotation, 1200f); while (true) { float targetDistance = Vector3.Distance(transform.position, PlugTransform.position); float snapSpeed = Mathf.Clamp(SnapToHandSpeed * targetDistance, SnapToHandSpeed, SnapToHandSpeed + 5f); positionSnap.SnapToTarget(transform.position + GetComponent <Rigidbody>().velocity, snapSpeed); if (positionSnap.HasReachedTarget) { break; } yield return(new WaitForFixedUpdate()); } positionSnap.closeEnoughDistance = originalCloseEnough; ResetPlugTransform(); yield return(new WaitForEndOfFrame()); snappingEnabled = true; }
private void OnTouchpadAxisChanged(object sender, ControllerInteractionEventArgs e) { if (!rotationSnap.HasReachedTarget) { return; } if (e.touchpadAxis.x > AxisChangeBeforeRotate) { Quaternion targetRotation = transform.localRotation * Quaternion.AngleAxis(SelectionStep * 90f, Vector3.up); rotationSnap.SnapToTarget(targetRotation, RotationSpeed, InterpolationType.Linear); } else if (e.touchpadAxis.x < -AxisChangeBeforeRotate) { Quaternion targetRotation = transform.localRotation * Quaternion.AngleAxis(-SelectionStep * 90f, Vector3.up); rotationSnap.SnapToTarget(targetRotation, RotationSpeed, InterpolationType.Linear); } }
private IEnumerator SnapToSphere() { yield return(null); LockedPlug.PlugTransform.SetParent(null); while (LockedPlug != null) { Vector3 toGrabber = lockedPlugGrabber.controllerAttachPoint.position - Sphere.position; targetPosition = Sphere.position + toGrabber.normalized * Sphere.lossyScale.z * Radius; targetRotation = Quaternion.LookRotation(-toGrabber); rotationSnap.SnapToTarget(targetRotation, RotationSnapSpeed); positionSnap.SnapToTarget(targetPosition, PositionSnapSpeed); yield return(new WaitForEndOfFrame()); } }