/// <summary> /// Moves the camera left/right until it reaches the target /// </summary> void MoveX_() { switch (_xMovement) { // move left case MovementDirectionX.Left: transform.localPosition -= new Vector3(_xSpeed * Time.deltaTime * SpeedAdjustment, 0, 0); // stop when target reached if (transform.localPosition.x < _targetPosition.x) { transform.localPosition = new Vector3(_targetPosition.x, transform.localPosition.y, transform.localPosition.z); _xMovement = MovementDirectionX.None; } break; // move right case MovementDirectionX.Right: transform.localPosition += new Vector3(_xSpeed * Time.deltaTime * SpeedAdjustment, 0, 0); // stop when target reached if (transform.localPosition.x > _targetPosition.x) { transform.localPosition = new Vector3(_targetPosition.x, transform.localPosition.y, transform.localPosition.z); _xMovement = MovementDirectionX.None; } break; } }
/// <summary> /// Start the movement /// </summary> /// <param name="targetPosition">Where to move to (where the camera should end up)</param> public void StartMovement(Vector2 targetPosition, float targetZoom) { // get the differences in X and Y directions var xDifference = Math.Abs(targetPosition.x - transform.localPosition.x); var yDifference = Math.Abs(targetPosition.y - transform.localPosition.y); var zDifference = Math.Abs(targetZoom - TheCamera.orthographicSize); // calculate the speed to move at var max = (Math.Max(Math.Max(xDifference, yDifference), zDifference)); _xSpeed = xDifference / max; _ySpeed = yDifference / max; _zoomSpeed = zDifference / max; _targetPosition = targetPosition; _targetZoom = targetZoom; // get the original movement if (_targetPosition.x != transform.localPosition.x) { _xMovement = _targetPosition.x > transform.localPosition.x ? MovementDirectionX.Right : MovementDirectionX.Left; } if (_targetPosition.y != transform.localPosition.y) { _yMovement = _targetPosition.y > transform.localPosition.y ? MovementDirectionY.Up : MovementDirectionY.Down; } if (_targetZoom != TheCamera.orthographicSize) { _zoomDirection = _targetZoom < TheCamera.orthographicSize ? ZoomDirection.In : ZoomDirection.Out; } // set values _callbackCalled = false; }