2D Player 조작2

Jihyun·2023년 12월 5일
0

Unity 2D

목록 보기
2/10

Slippery

Physics Material 2D

Jump를 하고 난 후에 플레이어가 콜라이더 마찰 때문에 그 자리에 고정되어 있는 경우가 나타난다.
프로젝트창에서 [Create]-[2D]-[Physics Material 2D]를 생성한 뒤에 Friction 값을 0으로 설정한다.

생성한 머터리얼을 플레이어의 Rigidbody2D의 Material에 할당하면 문제를 해결할 수 있다.

Dash

dashTime -= Time.deltaTime;
dashCooldownTimer -= Time.deltaTime;

if (Input.GetKeyDown(KeyCode.LeftShift) && dashCooldownTimer < 0)
{
          DashAbility();
}

if(dashTime > 0)
{
          rb.velocity = new Vector2(xInput * dashSpeed, 0);
}
else
{
            
          rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);

}        

Update()에서 Dash에 관한 시간 변수에 프레임 시간을 빼준다.
Dash 키가 눌렸으면서 Dash 쿨타임이 지났을 때의 조건을 만족하면, 다시 Dash 시간 변수들의 값을 초기화 시키고 dashTime 동안 dashSpeed 만큼의 속도를 곱해준다.

Attack

private bool isAttacking;
private int comboCounter;

if (Input.GetKeyDown(KeyCode.Mouse0))
{
    	isAttacking = true;
}

animator.SetBool("isAttacking", isAttacking);

Combo Attack

private float comboTime = 0.3f;
private float comboTimeWindow;
private int comboCounter;

comboTimeWindow -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Mouse0))
{
		if (comboTimeWindow < 0)
    	{
            comboCounter = 0;
    	}
    	isAttacking = true;
        comboTimeWindow = comboTime;
}
animator.SetBool("isAttacking", isAttacking);

Update()에서 Combo가 가능한지 판단하는 시간 변수에 Time.deltaTime을 빼준다.
마우스 왼쪽 버튼을 클릭했을 때, Combo가 가능한 시간이라면 comboCounter를 0으로 설정, 첫 번째 공격 애니메이션을 재생할 수 있게 만들어 준다.
isAttacking의 값이 true가 되면 공격 애니메이션을 재생

public void AttackOver()
{
        isAttacking = false;
        comboCounter++;

        if(comboCounter > 2)
        {
            comboCounter = 0;
        }              
            
}

공격 애니메이션의 마지막 프레임 애니메이션 이벤트를 설정하였다.
isAttacking의 값을 다시 false로 돌려 공격이 끝났음을 알리고 comboCount를 증가시킨다.

profile
잊어버려도 다시 리마인드 할 수 있도록 공부한 것을 기록합니다

0개의 댓글