Nicel193 / MFPCDocs

0 stars 0 forks source link

Running left, right not possible while running #1

Closed Johnney1337 closed 9 months ago

Johnney1337 commented 10 months ago

Hello :) I bought the MFPC and have a problem. I can't use the left and right movement while running. Any idea how to fix/change it?

Nicel193 commented 10 months ago

Hello :)

This is not a bug, this is how the control was designed.

But if you want the hover to be able to run left and right, here is the updated code:

using MFPC.PlayerStats;
using UnityEngine;

namespace MFPC
{
    /// <summary>
    /// Allows the player to move forward only
    /// </summary>
    public class MFPCRun : PlayerGroundedState
    {
        private PlayerStamina _playerStamina;
        private float _playerDataRunSpeed;

        public MFPCRun(Player player, PlayerStateMachine stateMachine, PlayerData playerData, MFPCPlayerRotation playerRotation) : base(
            player, stateMachine, playerData, playerRotation)
        {
            player.TryGetComponent(out _playerStamina);
        }

        public override bool IsChanged()
        {
            return TryCheckRunAbility();
        }

        public override void Enter()
        {
            base.Enter();

            player.Input.OnJumpAction += OnJumpEvent;
        }

        public override void Update()
        {
            base.Update();

            RunPlayer();

            if (ChangeToMove()) stateMachine.ChangeState(stateMachine.MovementState);

            if (player.CharacterController.isGrounded)
            {
                player.ChangeMoveCondition(player.Input.MoveDirection != Vector2.zero ? MoveConditions.Run : MoveConditions.Idle);
            }
        }

        public override void Exit()
        {
            base.Exit();

            player.Input.OnJumpAction -= OnJumpEvent;
        }

        private void RunPlayer()
        {
            Vector3 moveDirection = new Vector3(player.Input.MoveDirection.x, 0.0f, player.Input.MoveDirection.y);

            player.Movement.MoveHorizontal(moveDirection, playerData.RunSpeed);
        }

        /// <summary>
        /// Check if the player has a stamina component, if so, checks if it does
        /// </summary>
        /// <returns></returns>
        private bool TryCheckRunAbility()
        {
            if (_playerStamina != null && _playerStamina.enabled)
            {
                return _playerStamina.RunAbility;
            }

            return true;
        }

        private bool ChangeToMove()
        {
            return !player.Input.IsSprint || !TryCheckRunAbility() || player.Input.MoveDirection.y < 0f;
        }
    }
}

using UnityEngine;

namespace MFPC
{
    /// <summary>
    /// The basic movement script allows you to move, turn the camera horizontally to direct the position
    /// </summary>
    public class MFPCMovement : PlayerGroundedState
    {
        public MFPCMovement(Player player, PlayerStateMachine stateMachine, PlayerData playerData, MFPCPlayerRotation playerRotation) : base(
            player, stateMachine, playerData, playerRotation)
        {
        }

        public override void Enter()
        {
            base.Enter();

            player.Input.OnJumpAction += OnJumpEvent;
        }

        public override void Update()
        {
            base.Update();

            MovePlayer();

            if (player.CharacterController.isGrounded)
            {
                //Set the current movement state
                bool isMoving = player.Input.MoveDirection != Vector2.zero;
                player.ChangeMoveCondition(isMoving ? MoveConditions.Walk : MoveConditions.Idle);

                if (ChangeToRun(isMoving)) stateMachine.ChangeState(stateMachine.RunState);
                if (player.Input.LeanDirection != 0) stateMachine.ChangeState(stateMachine.LeanState);
            }
        }

        public override void Exit()
        {
            base.Exit();

            player.Input.OnJumpAction -= OnJumpEvent;
        }

        /// <summary>
        /// Responsible for moving the character using the "Character Controller" component
        /// </summary>
        private void MovePlayer()
        {
            player.Movement.MoveHorizontal(new Vector3(player.Input.MoveDirection.x, 0.0f, player.Input.MoveDirection.y),
                    playerData.WalkSpeed);
        }

        private bool ChangeToRun(bool isMoving)
        {
            return player.Input.IsSprint && isMoving && player.Input.MoveDirection.y > 0f;
        }
    }
}

using MFPC.Input.PlayerInput;
using UnityEngine;

namespace MFPC.Camera
{
    public class RunFovCameraEffect : ICameraModule
    {
        private UnityEngine.Camera _camera;
        private IPlayerInput _playerInput;
        private PlayerData _playerData;
        private Player _player;
        private float _initialFov;
        private float _time;

        public RunFovCameraEffect(UnityEngine.Camera camera, IPlayerInput playerInput, PlayerData playerData,
            Player player)
        {
            _camera = camera;
            _playerInput = playerInput;
            _playerData = playerData;
            _player = player;

            _initialFov = _camera.fieldOfView;
        }

        public void Update()
        {
            if (_camera == null) return;

            _time += (Time.deltaTime * _playerData.SpeedChangeFOV) * (IsIncreaseFOV() ? 1 : -1);
            _time = Mathf.Clamp01(_time);

            _camera.fieldOfView = Mathf.Lerp(_initialFov, _playerData.RunFOV, _time);
        }

        private bool IsIncreaseFOV() => _player.CurrentMoveCondition == MoveConditions.Run
                                        && _player.CurrentMoveCondition != MoveConditions.Climb
                                        && _player.CurrentMoveCondition != MoveConditions.Fell
                                        && _player.CurrentMoveCondition != MoveConditions.Lean;
    }
}

Here I limited running backwards, the character can only run to the right, left and straight. I hope this is what you needed ;)

Johnney1337 commented 9 months ago

Thank you very much 👍