0

I'm trying to make my player rolling using the new input system but i got 2 errors and the player stuck moving with out responding to any input.

errors are: enter image description here

the code are as follow. MovementToPositionEvent Code: enter image description here

MovementToPosition Code: enter image description here

AnimatePlayer Code:

private void OnEnable()
{
    _player.IdleEvents.OnIdle += IdleEvent_OnIdle;
   
    _player.AimWeaponEvents.OnWeaponAim += AimWeaponEvent_OnWeaponAim;
    
    _player.MovementByVelocityEvents.OnMovementByVelocity += MovementByVelocityEvent_OnMovementByVelocity;
   
    _player.MovementToPositionEvents.OnMovementToPosition += MovementToPositionEvent_OnMovementToPosition;
}

private void OnDisable()
{
    _player.IdleEvents.OnIdle -= IdleEvent_OnIdle;
   
    _player.AimWeaponEvents.OnWeaponAim -= AimWeaponEvent_OnWeaponAim;
   
    _player.MovementByVelocityEvents.OnMovementByVelocity -= MovementByVelocityEvent_OnMovementByVelocity;
  
    _player.MovementToPositionEvents.OnMovementToPosition -= MovementToPositionEvent_OnMovementToPosition;
}

private void MovementToPositionEvent_OnMovementToPosition(MovementToPositionEvent movementToPositionEvent, MovementToPositionArgs movementToPositionArgs)
{
    InitializeAimAnimationParameters();
    InitializeRollAnimationParameters();

    SetMovementToPositionAnimationParameters(movementToPositionArgs);
}

private void InitializeRollAnimationParameters()
{
    _player.Animators.SetBool(Settings.RollDown, false);
    _player.Animators.SetBool(Settings.RollLeft, false);
    _player.Animators.SetBool(Settings.RollRight, false);
    _player.Animators.SetBool(Settings.RollUp, false);
}

private void SetMovementToPositionAnimationParameters(MovementToPositionArgs movementToPositionArgs)
{
    if (movementToPositionArgs.IsRolling)
    {
        if (movementToPositionArgs.MoveDirection.x > 0.0f)
            _player.Animators.SetBool(Settings.RollRight, true);
        else if (movementToPositionArgs.MoveDirection.x < 0.0f)
            _player.Animators.SetBool(Settings.RollLeft, true);
        else if (movementToPositionArgs.MoveDirection.y > 0.0f)
            _player.Animators.SetBool(Settings.RollUp, true);
        else if (movementToPositionArgs.MoveDirection.y < 0.0f)
            _player.Animators.SetBool(Settings.RollDown, true);
    }
}

PlayerControl Code:

    private void Awake()
{
    _playerInput = new PlayerInput();

    _player = GetComponent<Player>();

    _moveSpeed = _movementDetails.GetMoveSpeed();
}

private void Start()
{
    _waitForFixedUpdate = new WaitForFixedUpdate();
}

private void Update()
{
    if (_isPlayerRolling)
        return;

    MovementInput();

    WeaponInput();

    PlayerRollCooldownTimer();
}

private void OnEnable()
{
    _playerInput.Enable();
}

private void OnDisable()
{
    _playerInput.Disable();
}

private void MovementInput()
{
    Vector2 inputVector = _playerInput.Player.Movement.ReadValue<Vector2>();

    _playerRoll = _playerInput.Player.Rolling.ReadValue<float>() == 1 ? true : false;

    Debug.Log(_playerRoll.ToString());


    if (inputVector != Vector2.zero)
    {
        if (!_playerRoll)
        {
            _player.MovementByVelocityEvents.CallMovementByVelocityEvent(inputVector, _moveSpeed);
        }
        else if (_playerRollCooldownTimer <= 0.0f)
        {
            Debug.Log(_playerRollCooldownTimer.ToString());
            PlayerRoll((Vector3)inputVector);
        }
    }
    else 
    { 
        _player.IdleEvents.CallIdleEvent();
    }
}

private void PlayerRoll(Vector3 direction)
{
    _playerRollCoroutine = StartCoroutine(PlayerRollRoutine(direction));
}

private IEnumerator PlayerRollRoutine(Vector3 direction)
{
    float minDistance = 0.2f;

    _isPlayerRolling = true;

    Vector3 targetPosition = _player.transform.position + (Vector3)direction * _movementDetails.RollDistance;

    while (Vector3.Distance(_player.transform.position, targetPosition) > minDistance)
    {
        _player.MovementToPositionEvents.CallMovementToPositionEvent(targetPosition, _player.transform.position, _movementDetails.RollSpeed, direction, _isPlayerRolling);

        yield return _waitForFixedUpdate;
    }

    _isPlayerRolling = false;
    _playerRollCooldownTimer = _movementDetails.RollCooldownTime;
    _player.transform.position = targetPosition;
}

I don't know what should I do appreciate if some one can help me.

Thanks

derHugo
  • 83,094
  • 9
  • 75
  • 115
ali mahdi
  • 17
  • 5
  • 1
    [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – derHugo Feb 17 '23 at 08:31
  • There are a lot of things that can be `null` there resulting in things not listening to those events -> debug that! Also in general do **not** use `==` for comparing `float`! See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Compare floats in Unity](https://stackoverflow.com/a/67936871/7111561) – derHugo Feb 17 '23 at 08:34

0 Answers0