IEnumerator InsideTriggerRoutine() { yield return(ProCamera2D.GetYield()); _tempExclusivePoint = VectorHV(Vector3H(ProCamera2D.CameraTargetPosition), Vector3V(ProCamera2D.CameraTargetPosition)); while (_insideTrigger) { _exclusiveInfluencePercentage = ExclusiveInfluencePercentage; var distancePercentage = GetDistanceToCenterPercentage(new Vector2(Vector3H(ProCamera2D.TargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint))); var vectorFromPointToFocus = new Vector2(Vector3H(ProCamera2D.TargetsMidPoint) + Vector3H(ProCamera2D.TargetsMidPoint) - Vector3H(ProCamera2D.PreviousTargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint) + Vector3V(ProCamera2D.TargetsMidPoint) - Vector3V(ProCamera2D.PreviousTargetsMidPoint)) - new Vector2(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)); if (distancePercentage == 0) { ProCamera2D.ExclusiveTargetPosition = Vector3.SmoothDamp(_tempExclusivePoint, VectorHV(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)), ref _exclusivePointVelocity, InfluenceSmoothness); _tempExclusivePoint = ProCamera2D.ExclusiveTargetPosition.Value; _influence = -vectorFromPointToFocus * (1 - distancePercentage); ProCamera2D.ApplyInfluence(_influence); } else { _influence = Vector2.SmoothDamp(_influence, -vectorFromPointToFocus * (1 - distancePercentage), ref _velocity, InfluenceSmoothness); ProCamera2D.ApplyInfluence(_influence); _tempExclusivePoint = VectorHV(Vector3H(ProCamera2D.CameraTargetPosition), Vector3V(ProCamera2D.CameraTargetPosition)); } yield return(ProCamera2D.GetYield()); } }
IEnumerator InsideTriggerRoutine() { var waitForFixedUpdate = new WaitForFixedUpdate(); yield return((ProCamera2D.UpdateType == UpdateType.FixedUpdate) ? waitForFixedUpdate : null); _tempExclusivePoint = new Vector2(Vector3H(ProCamera2D.CameraTargetPosition), Vector3V(ProCamera2D.CameraTargetPosition)); while (_insideTrigger) { _exclusiveInfluencePercentage = ExclusiveInfluencePercentage; var distancePercentage = GetDistanceToCenterPercentage(new Vector2(Vector3H(ProCamera2D.TargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint))); var vectorFromPointToFocus = new Vector2(Vector3H(ProCamera2D.TargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint)) - new Vector2(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)); if (distancePercentage == 0) { ProCamera2D.ExclusiveTargetPosition = Vector2.SmoothDamp(_tempExclusivePoint, new Vector2(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)), ref _velocity, InfluenceSmoothness, Mathf.Infinity, Time.deltaTime); _tempExclusivePoint = ProCamera2D.ExclusiveTargetPosition.Value; _influence = -vectorFromPointToFocus * (1 - distancePercentage); ProCamera2D.ApplyInfluence(_influence); } else { _influence = Vector2.SmoothDamp(_influence, -vectorFromPointToFocus * (1 - distancePercentage), ref _velocity, InfluenceSmoothness, Mathf.Infinity, Time.deltaTime); ProCamera2D.ApplyInfluence(_influence); _tempExclusivePoint = new Vector2(Vector3H(ProCamera2D.CameraTargetPosition), Vector3V(ProCamera2D.CameraTargetPosition)); } yield return((ProCamera2D.UpdateType == UpdateType.FixedUpdate) ? waitForFixedUpdate : null); } }
IEnumerator InsideTriggerRoutine() { yield return(ProCamera2D.GetYield()); var previousDistancePercentage = 1f; _tempExclusivePoint = VectorHV(Vector3H(ProCamera2D.transform.position), Vector3V(ProCamera2D.transform.position)); while (_insideTrigger) { _exclusiveInfluencePercentage = ExclusiveInfluencePercentage; var distancePercentage = GetDistanceToCenterPercentage(new Vector2(Vector3H(ProCamera2D.TargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint))); var vectorFromPointToFocus = new Vector2(Vector3H(ProCamera2D.TargetsMidPoint) + Vector3H(ProCamera2D.TargetsMidPoint) - Vector3H(ProCamera2D.PreviousTargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint) + Vector3V(ProCamera2D.TargetsMidPoint) - Vector3V(ProCamera2D.PreviousTargetsMidPoint)) - new Vector2(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)); if (distancePercentage == 0) { var exclusiveTargetPosition = VectorHV(Vector3H(FocusPoint.position), Vector3V(FocusPoint.position)); ProCamera2D.ExclusiveTargetPosition = Vector3.SmoothDamp(_tempExclusivePoint, exclusiveTargetPosition, ref _exclusivePointVelocity, InfluenceSmoothness); _tempExclusivePoint = ProCamera2D.ExclusiveTargetPosition.Value; _influence = -vectorFromPointToFocus * (1 - distancePercentage); if (Mode == TriggerInfluenceMode.HorizontalAxis) { _influence.y = 0; } else if (Mode == TriggerInfluenceMode.VerticalAxis) { _influence.x = 0; } ProCamera2D.ApplyInfluence(_influence); } else { if (previousDistancePercentage == 0) { _influence = new Vector2(Vector3H(ProCamera2D.CameraTargetPositionSmoothed), Vector3V(ProCamera2D.CameraTargetPositionSmoothed)) - new Vector2(Vector3H(ProCamera2D.TargetsMidPoint) + Vector3H(ProCamera2D.TargetsMidPoint) - Vector3H(ProCamera2D.PreviousTargetsMidPoint), Vector3V(ProCamera2D.TargetsMidPoint) + Vector3V(ProCamera2D.TargetsMidPoint) - Vector3V(ProCamera2D.PreviousTargetsMidPoint)) + new Vector2(Vector3H(ProCamera2D.ParentPosition), Vector3V(ProCamera2D.ParentPosition)); } _influence = Vector2.SmoothDamp(_influence, -vectorFromPointToFocus * (1 - distancePercentage), ref _velocity, InfluenceSmoothness, Mathf.Infinity, Time.deltaTime); if (Mode == TriggerInfluenceMode.HorizontalAxis) { _influence.y = 0; } else if (Mode == TriggerInfluenceMode.VerticalAxis) { _influence.x = 0; } ProCamera2D.ApplyInfluence(_influence); _tempExclusivePoint = VectorHV(Vector3H(ProCamera2D.CameraTargetPosition), Vector3V(ProCamera2D.CameraTargetPosition)) + VectorHV(Vector3H(ProCamera2D.ParentPosition), Vector3V(ProCamera2D.ParentPosition)); } previousDistancePercentage = distancePercentage; yield return(ProCamera2D.GetYield()); } }
IEnumerator OutsideTriggerRoutine() { yield return(ProCamera2D.GetYield()); while (!_insideTrigger && _influence != Vector2.zero) { _influence = Vector2.SmoothDamp(_influence, Vector2.zero, ref _velocity, InfluenceSmoothness, 1000f, Time.deltaTime); ProCamera2D.ApplyInfluence(_influence); yield return(ProCamera2D.GetYield()); } }
void ApplyInfluence(float deltaTime) { var mousePosViewport = ProCamera2D.GameCamera.ScreenToViewportPoint(Input.mousePosition); var mousePosViewportH = mousePosViewport.x.Remap(0, 1, -1, 1); var mousePosViewportV = mousePosViewport.y.Remap(0, 1, -1, 1); var hInfluence = mousePosViewportH * MaxHorizontalInfluence; var vInfluence = mousePosViewportV * MaxVerticalInfluence; _influence = Vector2.SmoothDamp(_influence, new Vector2(hInfluence, vInfluence), ref _velocity, InfluenceSmoothness, Mathf.Infinity, deltaTime); ProCamera2D.ApplyInfluence(_influence); }
IEnumerator OutsideTriggerRoutine() { var waitForFixedUpdate = new WaitForFixedUpdate(); yield return((ProCamera2D.UpdateType == UpdateType.FixedUpdate) ? waitForFixedUpdate : null); while (!_insideTrigger && _influence != Vector2.zero) { _influence = Vector2.SmoothDamp(_influence, Vector2.zero, ref _velocity, InfluenceSmoothness, Mathf.Infinity, Time.deltaTime); ProCamera2D.ApplyInfluence(_influence); yield return((ProCamera2D.UpdateType == UpdateType.FixedUpdate) ? waitForFixedUpdate : null); } }
void ApplyInfluence(float deltaTime) { var currentHVel = (Vector3H(ProCamera2D.TargetsMidPoint) - Vector3H(ProCamera2D.PreviousTargetsMidPoint)) / deltaTime; var currentVVel = (Vector3V(ProCamera2D.TargetsMidPoint) - Vector3V(ProCamera2D.PreviousTargetsMidPoint)) / deltaTime; if (Progressive) { currentHVel = Mathf.Clamp(currentHVel * SpeedMultiplier, -LeftFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x, RightFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x); currentVVel = Mathf.Clamp(currentVVel * SpeedMultiplier, -BottomFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y, TopFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y); if (MaintainInfluenceOnStop) { if ((Mathf.Sign(currentHVel) == 1 && currentHVel < _hVel) || (Mathf.Sign(currentHVel) == -1 && currentHVel > _hVel) || (Mathf.Abs(currentHVel) < EPSILON)) { currentHVel = _hVel; } if ((Mathf.Sign(currentVVel) == 1 && currentVVel < _vVel) || (Mathf.Sign(currentVVel) == -1 && currentVVel > _vVel) || (Mathf.Abs(currentVVel) < EPSILON)) { currentVVel = _vVel; } } } else { if (MaintainInfluenceOnStop) { bool switchedHorizontalDirection; if (!_isFirstHorizontalCameraMovement && !(Mathf.Abs(currentHVel) < EPSILON)) { _isFirstHorizontalCameraMovement = true; switchedHorizontalDirection = true; } else { switchedHorizontalDirection = Mathf.Sign(currentHVel) != Mathf.Sign(_targetHVel); } if (!(Mathf.Abs(currentHVel) < EPSILON) && switchedHorizontalDirection) { _targetHVel = (currentHVel < 0f ? -LeftFocus : RightFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.x; } currentHVel = _targetHVel; bool switchedVerticalDirection; if (!_isFirstVerticalCameraMovement && !(Mathf.Abs(currentVVel) < EPSILON)) { _isFirstVerticalCameraMovement = true; switchedVerticalDirection = true; } else { switchedVerticalDirection = Mathf.Sign(currentVVel) != Mathf.Sign(_targetVVel); } if (!(Mathf.Abs(currentVVel) < EPSILON) && switchedVerticalDirection) { _targetVVel = (currentVVel < 0f ? -BottomFocus : TopFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.y; } currentVVel = _targetVVel; } else { if (!(Mathf.Abs(currentHVel) < EPSILON)) { currentHVel = (currentHVel < 0f ? -LeftFocus : RightFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.x; } else { currentHVel = 0; } if (!(Mathf.Abs(currentVVel) < EPSILON)) { currentVVel = (currentVVel < 0f ? -BottomFocus : TopFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.y; } else { currentVVel = 0; } } } // We need to clamp the values again to account for camera zooms currentHVel = Mathf.Clamp(currentHVel, -LeftFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x, RightFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x); currentVVel = Mathf.Clamp(currentVVel, -BottomFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y, TopFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y); // Smooth the values _hVel = Mathf.SmoothDamp(_hVel, currentHVel, ref _hVelSmooth, TransitionSmoothness); _vVel = Mathf.SmoothDamp(_vVel, currentVVel, ref _vVelSmooth, TransitionSmoothness); // Apply the influence ProCamera2D.ApplyInfluence(new Vector2(_hVel, _vVel)); }
void ApplyInfluence() { var deltaTime = (ProCamera2D.UpdateType == UpdateType.FixedUpdate) ? Time.fixedDeltaTime : Time.deltaTime; if (Time.deltaTime < .0001f) { return; } var currentHVel = (Vector3H(ProCamera2D.TargetsMidPoint) - Vector3H(ProCamera2D.PreviousTargetsMidPoint)) / deltaTime; var currentVVel = (Vector3V(ProCamera2D.TargetsMidPoint) - Vector3V(ProCamera2D.PreviousTargetsMidPoint)) / deltaTime; if (Progressive) { currentHVel = Mathf.Clamp(currentHVel * SpeedMultiplier, -LeftFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x, RightFocus * ProCamera2D.ScreenSizeInWorldCoordinates.x); currentVVel = Mathf.Clamp(currentVVel * SpeedMultiplier, -BottomFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y, TopFocus * ProCamera2D.ScreenSizeInWorldCoordinates.y); if (MaintainInfluenceOnStop) { if ((Mathf.Sign(currentHVel) == 1 && currentHVel < _hVel) || (Mathf.Sign(currentHVel) == -1 && currentHVel > _hVel) || (Mathf.Abs(currentHVel) < EPSILON)) { currentHVel = _hVel; } if ((Mathf.Sign(currentVVel) == 1 && currentVVel < _vVel) || (Mathf.Sign(currentVVel) == -1 && currentVVel > _vVel) || (Mathf.Abs(currentVVel) < EPSILON)) { currentVVel = _vVel; } } } else { if (MaintainInfluenceOnStop) { bool switchedHorizontalDirection; if (!_isFirstHorizontalCameraMovement && !(Mathf.Abs(currentHVel) < EPSILON)) { _isFirstHorizontalCameraMovement = true; switchedHorizontalDirection = true; } else { switchedHorizontalDirection = Mathf.Sign(currentHVel) != Mathf.Sign(_targetHVel); } if (!(Mathf.Abs(currentHVel) < EPSILON) && switchedHorizontalDirection) { _targetHVel = (currentHVel < 0f ? -LeftFocus : RightFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.x; } currentHVel = _targetHVel; bool switchedVerticalDirection; if (!_isFirstVerticalCameraMovement && !(Mathf.Abs(currentVVel) < EPSILON)) { _isFirstVerticalCameraMovement = true; switchedVerticalDirection = true; } else { switchedVerticalDirection = Mathf.Sign(currentVVel) != Mathf.Sign(_targetVVel); } if (!(Mathf.Abs(currentVVel) < EPSILON) && switchedVerticalDirection) { _targetVVel = (currentVVel < 0f ? -BottomFocus : TopFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.y; } currentVVel = _targetVVel; } else { if (!(Mathf.Abs(currentHVel) < EPSILON)) { currentHVel = (currentHVel < 0f ? -LeftFocus : RightFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.x; } else { currentHVel = 0; } if (!(Mathf.Abs(currentVVel) < EPSILON)) { currentVVel = (currentVVel < 0f ? -BottomFocus : TopFocus) * ProCamera2D.ScreenSizeInWorldCoordinates.y; } else { currentVVel = 0; } } } _hVel = Mathf.SmoothDamp(_hVel, currentHVel, ref _hVelSmooth, TransitionSmoothness); _vVel = Mathf.SmoothDamp(_vVel, currentVVel, ref _vVelSmooth, TransitionSmoothness); // Apply the influence ProCamera2D.ApplyInfluence(new Vector2(_hVel, _vVel)); }