/// <summary> /// Computes the current aim direction /// </summary> public virtual Vector2 GetCurrentAim() { switch (AimControl) { case AimControls.Off: _currentAim = Vector2.zero; break; case AimControls.Script: // in that mode we simply use _currentAim, as set by the SetAim method break; case AimControls.PrimaryMovement: _currentAim = PrimaryMovement; break; case AimControls.SecondaryMovement: _currentAim = SecondaryMovement; break; case AimControls.Mouse: _mousePosition = Input.mousePosition; _mousePosition.z = 10; _direction = _mainCamera.ScreenToWorldPoint(_mousePosition); _direction.z = CurrentPosition.z; _currentAim = _direction - CurrentPosition; break; default: _currentAim = Vector2.zero; break; } // we compute our angle in degrees CurrentAngle = Mathf.Atan2(_currentAim.y, _currentAim.x) * Mathf.Rad2Deg; // we clamp our raw angle if needed if ((CurrentAngle < MinimumAngle) || (CurrentAngle > MaximumAngle)) { float minAngleDifference = Mathf.DeltaAngle(CurrentAngle, MinimumAngle); float maxAngleDifference = Mathf.DeltaAngle(CurrentAngle, MaximumAngle); CurrentAngle = (Mathf.Abs(minAngleDifference) < Mathf.Abs(maxAngleDifference)) ? MinimumAngle : MaximumAngle; } // we round to the closest angle if (RotationMode == RotationModes.Strict4Directions || RotationMode == RotationModes.Strict8Directions) { CurrentAngle = MMMaths.RoundToClosest(CurrentAngle, _possibleAngleValues); } // we clamp the final value CurrentAngle = Mathf.Clamp(CurrentAngle, MinimumAngle, MaximumAngle); // we return our aim vector _currentAim = (_currentAim.magnitude == 0f) ? Vector2.zero : MMMaths.RotateVector2(Vector2.right, CurrentAngle); return(_currentAim); }
/// <summary> /// Computes the current aim direction /// </summary> public virtual Vector2 GetCurrentAim() { switch (AimControl) { case AimControls.Off: _currentAim = Vector2.zero; break; case AimControls.Script: break; case AimControls.PrimaryMovement: _currentAim = PrimaryMovement; break; case AimControls.SecondaryMovement: _currentAim = SecondaryMovement; break; case AimControls.Mouse: _mousePosition = Input.mousePosition; _mousePosition.z = 10; _direction = _mainCamera.ScreenToWorldPoint(_mousePosition); _direction.z = CurrentPosition.z; _currentAim = _direction - CurrentPosition; break; default: _currentAim = Vector2.zero; break; } float currentAngle = Mathf.Atan2(_currentAim.y, _currentAim.x) * Mathf.Rad2Deg; if (RotationMode == RotationModes.Strict4Directions || RotationMode == RotationModes.Strict8Directions) { currentAngle = MMMaths.RoundToClosest(currentAngle, _possibleAngleValues); } CurrentAngle = Mathf.Clamp(CurrentAngle, MinimumAngle, MaximumAngle); _currentAim = (_currentAim.magnitude == 0f) ? Vector2.zero : MMMaths.RotateVector2(Vector2.right, currentAngle); return(_currentAim); }