private void UpdateLine( Transform controllerTransform, BaseTeleportDetector.Result selectionResult, out Vector3 start, out Vector3 end, out Vector3 control) { // Start point of line. start = controllerTransform.position + (controllerTransform.forward * lineStartOffset); // End line at selection or max distance. end = selectionResult.selection; // We can only offset if the line is long enough for it. float lineLength = (end - start).magnitude; float clampedEndOffset = Mathf.Clamp(this.lineEndOffset, 0, lineLength); Vector3 lineEndVector = end - start - (controllerTransform.forward * clampedEndOffset); end = start + lineEndVector; // Get control point used to bend the line into an arc. control = ControlPointForLine( start, end, selectionResult.maxDistance, controllerTransform); }
protected void UpdateTarget(BaseTeleportDetector.Result selectionResult) { if (targetPrefab == null) { return; } if (target == null) { target = Instantiate(targetPrefab) as GameObject; } targetCurrentSize = Mathf.SmoothDamp(targetCurrentSize, targetGoalSize, ref targetSizeVelocity, targetScaleTransitionDuration); target.transform.localScale = new Vector3(targetCurrentSize, targetCurrentSize, targetCurrentSize); if (selectionResult.selectionIsValid) { target.transform.position = selectionResult.selection; // Make visible and grow the teleport target object when there is a valid selection. ShowTarget(); } else if (validHitFound) { // If there isn't a valid selection, but one was previously found and cached, show the // target at that position instead. target.transform.position = lastValidHitPosition; HideTarget(); } else { // Otherwise, just hide the teleport target. HideTarget(); } }
protected void UpdateTarget(BaseTeleportDetector.Result selectionResult) { if (target == null) { return; } if (selectionResult.selectionIsValid == false) { target.SetActive(false); return; } target.SetActive(true); target.transform.position = selectionResult.selection; }
// Update the visualization. public override void UpdateSelection( Transform controllerTransform, BaseTeleportDetector.Result selectionResult) { // Calculate the position for the 3 control verticies on the line. UpdateLine(controllerTransform, selectionResult, out start, out end, out control); // Update the target object's position at end of line. UpdateTarget(selectionResult); // Update the object used to represent the origin of the line. UpdateLineOrigin(selectionResult); // Update mesh renderers. UpdateMaterials(selectionResult.selectionIsValid, start, end, control); }
// Visualize the current selection. public override void UpdateSelection( Transform controllerTransform, BaseTeleportDetector.Result selectionResult) { // Calculate verticies that define our bezier arc. Vector3 start; Vector3 end; Vector3 control; // Calculate the position for the 3 verticies. UpdateLine(controllerTransform, selectionResult, out start, out end, out control); // Update the material on the line. UpdateLineMaterial(selectionResult.selectionIsValid, start, end, control); // Update the target objects position at end of line. UpdateTarget(selectionResult); }
protected void UpdateLineOrigin(BaseTeleportDetector.Result selectionResult) { if (lineOriginPrefab == null) { return; } if (lineOrigin == null) { lineOrigin = Instantiate(lineOriginPrefab) as GameObject; } if (lineOriginRenderer == null) { lineOriginRenderer = lineOrigin.GetComponent <MeshRenderer>(); } // Move the line origin prefab to the start of the line. lineOrigin.transform.position = start; // Show the lineOrigin if there is a valid selection. // Hide the lineOrigin if there is no valid selection and the line // is also hidden. if (lineOrigin && minLineLength == 0f) { lineOriginActive = selectionResult.selectionIsValid; } if (lineOriginActive) { lineOriginGoalSize = 1f; } else { lineOriginGoalSize = 0f; } lineOriginCurrentSize = Mathf.SmoothDamp(lineOriginCurrentSize, lineOriginGoalSize, ref lineOriginSizeVelocity, lineOriginScaleTransitionDuration); lineOrigin.transform.localScale = new Vector3(lineOriginCurrentSize, lineOriginCurrentSize, lineOriginCurrentSize); }
void Update() { if (IsConfigured() == false) { return; } currentController = GetControllerTransform(); // Complete active teleport transitions. if (IsTeleporting) { visualizer.OnTeleport(); // Update the visualization. visualizer.UpdateSelection(currentController, selectionResult); return; } // If rotation is allowed, handle player rotations. if (allowRotation) { HandlePlayerRotations(); } // If a teleport selection session has not started, check the appropriate // trigger to see if one should start. if (selectionIsActive == false) { if (teleportStartTrigger.TriggerActive()) { //if player is not ready for teleport then return if (Player.instance.currentState != Player.PlayerState.None) { return; } StartTeleportSelection(); } } // Get the current selection result from the detector. // float playerHeight = DetectPlayerHeight(); float playerHeight = Player.instance.playerHeight; selectionResult = detector.DetectSelection(currentController, playerHeight); // Update the visualization. visualizer.UpdateSelection(currentController, selectionResult); // If not actively teleporting, just return. if (selectionIsActive == false) { return; } // Check for the optional cancel trigger. if (teleportCancelTrigger != null && teleportCancelTrigger.TriggerActive() && !teleportCommitTrigger.TriggerActive()) { EndTeleportSelection(); return; } Vector3 nextPlayerPosition = Vector3.zero; // When trigger deactivates we finish the teleport. if (selectionIsActive && teleportCommitTrigger.TriggerActive()) { if (selectionResult.selectionIsValid) { detector.EndSelection(); visualizer.EndSelection(); nextPlayerPosition = new Vector3( selectionResult.selection.x, selectionResult.selection.y + playerHeight, selectionResult.selection.z); // Start a transition to move the player. //transition.StartTransition( // player, // currentController, // nextPlayerPosition); if (Player.instance.visualPlayer) { Player.instance.visualPlayer.transform.DOMove(nextPlayerPosition, 1f); } player.DOMove(nextPlayerPosition, 1f).OnComplete(() => { EndTeleportSelection(); }); } // EndTeleportSelection(); } }
private void UpdateLine(Transform controller, BaseTeleportDetector.Result selectionResult, out Vector3 start, out Vector3 end, out Vector3 control) { // Start point of line. start = controller.position + controller.up * lineStartOffset.y + controller.forward * lineStartOffset.z; // End line at selection or max distance. if (selectionResult.selectionIsValid) { validHitFound = true; end = lastValidHitPosition = selectionResult.selection; } else { // When there isn't a selection available, retract the line. Vector3 defaultEndPosition = start + forward * minLineLength; // If a valid hit was previously cached, interpolate from that position. if (validHitFound) { smoothEnd = lastValidHitPosition; defaultEndPosition.x = lastValidHitPosition.x; defaultEndPosition.z = lastValidHitPosition.z; validHitFound = false; // Otherwise, just retract the laser to its default length. } else { smoothEnd = defaultEndPosition; } end = Vector3.Lerp(smoothEnd, defaultEndPosition, Time.deltaTime * arcTransitionSpeed); } // In order to apply an offset to the arc's start angle and have the arc // ignore controller roll, we need to manually recalculate controller forward. // Get the current forward vector of the controller. forward = controller.forward; // Get the pitch of the controller. float pitch = Mathf.Rad2Deg * Mathf.Asin(forward.y); float absPitch = Mathf.Abs(pitch); // Safeguards to prevent undesired behavior around rotation poles. if (absPitch < MAX_PITCH_WITH_OFFSET) { float pitchBlend = 1 - Mathf.Clamp01((absPitch - (MAX_PITCH_WITH_OFFSET - MAX_PITCH_BLEND)) / MAX_PITCH_BLEND); // Apply the visual offset to the pitch of the arc. Blend the offset to // zero as controller pitch approaches -90 or 90 degrees. float angleOffset = pitchBlend * forwardAngleOffset; float yaw = Mathf.Rad2Deg * Mathf.Atan2(forward.x, forward.z); float pitchRad = Mathf.Deg2Rad * (angleOffset + pitch); Vector3 pitchVec = new Vector3(0, Mathf.Sin(pitchRad), Mathf.Cos(pitchRad)); // Calculate the axes of the forward vector in the appropriate order. forward = Quaternion.AngleAxis(yaw, Vector3.up) * pitchVec; } // Blend forward back to controller forward as the controller unrolls. float blend = Mathf.Clamp01(Vector3.Angle(controller.up, Vector3.up) / MAX_ROLL_TOLERANCE); forward = Vector3.Lerp(forward, controller.forward, blend); // Get control point used to bend the line into an arc. control = ControlPointForLine( start, end, forward); }
void Update() { if (IsConfigured() == false) { return; } currentController = GetControllerTransform(); // Complete active teleport transitions. if (IsTeleporting) { visualizer.OnTeleport(); // Update the visualization. visualizer.UpdateSelection(currentController, selectionResult); return; } // If rotation is allowed, handle player rotations. if (allowRotation) { HandlePlayerRotations(); } // If a teleport selection session has not started, check the appropriate // trigger to see if one should start. if (selectionIsActive == false) { if (teleportStartTrigger.TriggerActive()) { StartTeleportSelection(); } } // Get the current selection result from the detector. float playerHeight = DetectPlayerHeight(); selectionResult = detector.DetectSelection(currentController, playerHeight); // Update the visualization. visualizer.UpdateSelection(currentController, selectionResult); // If not actively teleporting, just return. if (selectionIsActive == false) { return; } // Check for the optional cancel trigger. if (teleportCancelTrigger != null && teleportCancelTrigger.TriggerActive() && !teleportCommitTrigger.TriggerActive()) { EndTeleportSelection(); return; } // When trigger deactivates we finish the teleport. if (selectionIsActive && teleportCommitTrigger.TriggerActive()) { if (selectionResult.selectionIsValid) { Vector3 nextPlayerPosition = new Vector3( selectionResult.selection.x, selectionResult.selection.y + playerHeight, selectionResult.selection.z); // Start a transition to move the player. transition.StartTransition( player, currentController, nextPlayerPosition); } EndTeleportSelection(); } }
/// Update the current visualized selection. public abstract void UpdateSelection( Transform controllerTransform, BaseTeleportDetector.Result selectionResult);
void Update() { if (IsConfigured() == false) { return; } // Ignore everything until teleport transition completes. if (IsTeleporting) { return; } // No teleport session started yet, let's check our triggers. if (selectionIsActive == false) { if (teleportStartTrigger.TriggerActive()) { selectionIsActive = true; StartTeleportSelection(); } } // Always process rotations until complete. if (allowRotation && HandlePlayerRotations()) { return; } // If not actively teleporting, just return. if (selectionIsActive == false) { return; } // Check for the optional cancel trigger. if (teleportCancelTrigger != null && teleportCancelTrigger.TriggerActive() && !teleportCommitTrigger.TriggerActive()) { EndTeleportSelection(); return; } Transform currentController = GetControllerTransform(); // Detect the teleport selection, and if it's valid for moving to. BaseTeleportDetector.Result selectionResult = detector.DetectSelection(currentController); // Update the visualization. visualizer.UpdateSelection(currentController, selectionResult); // When trigger deactivates we finish the teleport. if (selectionIsActive && teleportCommitTrigger.TriggerActive()) { if (selectionResult.selectionIsValid) { float playerHeight = DetectPlayersHeight(); Vector3 nextPlayerPosition = new Vector3( selectionResult.selection.x, selectionResult.selection.y + playerHeight, selectionResult.selection.z); // Start a transition to move the player. transition.StartTransition( player, currentController, nextPlayerPosition); } EndTeleportSelection(); } }