본문 바로가기
GGM/엔진

20230320 - 엔진 - Combo, Rolling

by DDongYeop 2023. 3. 21.
728x90

 

 

우선 AgentMovement에 SetRotation함수를 추가해주고, 매개 변수에 따라 보는 곳이 달라지도록 해주는 코드를 작성한다. 

    public void SetRotation(Vector3 targetPos)
    {
        Vector3 dir = targetPos - transform.position;
        dir.y = 0;
        transform.rotation = Quaternion.LookRotation(dir);
    }

 

 

이후 AgentInput에서 Rolling시킬 키를 만들어준다 

    private void UpdateRollingInput()
    {
        if (Input.GetButtonDown("Jump"))
        {
            OnRollingKeyPress?.Invoke();
        }
    }

 

 

이후 AttackState를 수정하여 Rolling을 위해 키보드 입력 안 받는 등 코드를 추가해준다

using Core;
using UnityEngine;

public class AttackState : CommonState
{
    [SerializeField] private float _keyDelay = 0.5f;
    
    private int _currentCombo = 1;
    private bool _canAttack = true;
    private float _keyTimer = 0; 
    
    private float _attackStartTime;
    [SerializeField] private float _attackSlideDuration = .2f, _attackSlideSpeed = .1f;
    
    public override void OnEnterState()
    {
        _agentInput.OnAttackKeyPress += OnAttackHandle;
        _animator.OnAnimationEndTrigger += OnAnimaionEnd;
        _agentInput.OnRollingKeyPress += OnRollingHandle;
        _currentCombo = 0;
        _canAttack = true;
        _animator.SetAttackState(true);

        _agentMovement.IsActiveMove = false; 

        Vector3 pos = _agentInput.GetMouseWorldPosition();
        _agentMovement.SetRotation(pos);
        OnAttackHandle();  
    }

    public override void OnExitState()
    {
        _agentInput.OnAttackKeyPress -= OnAttackHandle;
        _animator.OnAnimationEndTrigger -= OnAnimaionEnd;
        _agentInput.OnRollingKeyPress -= OnRollingHandle;
        _animator.SetAttackState(false);
        _animator.SetAttackTrigger(false);
        
        _agentMovement.IsActiveMove = true; 
    }

    private void OnRollingHandle()
    {
        _agentController.ChangeState(StateType.Rolling);
    }

    private void OnAnimaionEnd()
    {
        _canAttack = true;
        _keyTimer = _keyDelay;
    }

    public void OnAttackHandle()
    {
        if (_canAttack && _currentCombo < 3)
        {
            _attackStartTime = Time.time;
            _canAttack = false;
            _currentCombo++;
            _animator.SetAttackTrigger(true);
        }
    }

    public override void UpdateState()
    {
        if (Time.time < _attackStartTime + _attackSlideDuration) 
        {
            float timePassed = Time.time - _attackStartTime; 
            float lerpTime = timePassed / _attackSlideDuration;

            Vector3 targetSpeed = Vector3.Lerp(_agentController.transform.forward * _attackSlideSpeed, Vector3.zero, lerpTime);
            _agentMovement.SetMovementVelocity(targetSpeed);
        }
        
        if (_canAttack && _keyTimer > 0)
        {
            _keyTimer -= Time.deltaTime;
            if (_keyTimer <= 0)
                _agentController.ChangeState(StateType.Normal);
        }
    }
}

 

이후 AgentAnimator에서 Rolling관련 해서 추가해준다. 

    private readonly int _isRollingHash = Animator.StringToHash("is_rolling");
    
    public void SetRollingState(bool value)
    {
        _animator.SetBool(_isRollingHash, value);
    }

 

 

이후 RollingState를 만들어 스테이트 상황일떄의 코드를 전부 넣어준다 

using UnityEngine;

public class RollingState : CommonState
{
    [SerializeField] private float _rollingSpeed = 0.4f;
    
    public override void OnEnterState()
    {
        //마우스 보고 
        /*_animator.OnAnimationEndTrigger += RollingEndHandle;
        _agentMovement.IsActiveMove = false;
        _animator.SetRollingState(true);
        //어느 방향을 보고 회전시킬지 결정해야함, 마우스 보도록 
        Vector3 mousePos = _agentInput.GetMouseWorldPosition();
        Vector3 dir = mousePos - _agentController.transform.position;
        dir.y = 0;
        //_agentMovement.SetRotation(dir);
        _agentMovement.SetMovementVelocity(dir.normalized * _rollingSpeed);*/
        
        //키보드 사용 
        _animator.OnAnimationEndTrigger += RollingEndHandle;
        _agentMovement.IsActiveMove = false; 
        _animator.SetRollingState(true);
        _agentMovement.StopImmediately();
        _agentMovement.SetMovementVelocity(transform.forward * _rollingSpeed);
    }

    public override void OnExitState()
    {
        _animator.OnAnimationEndTrigger -= RollingEndHandle;
        _agentMovement.IsActiveMove = true;
        _animator.SetRollingState(false);
    }

    private void RollingEndHandle()
    {
        _agentMovement.StopImmediately();
        _agentController.ChangeState(Core.StateType.Normal);
    }
    
    public override void UpdateState()
    {
        //do nothing
    }
}
728x90

댓글