유니티/AA(3D 쿼터뷰 액션 게임)
AA(15) - 근접무기 공격 구현
무직백수취업준비생
2021. 9. 15. 03:24
728x90
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public enum Type { Melee, Range };
public Type type;
public int damage;
public float rate;
public BoxCollider meleeArea;
public TrailRenderer trailEffect;
}
스크립트를 새로 만들어서 변수들을 선언해줍니다.
근접무기 장착 시 활성화되는 오브젝트에 스크립트를 드래그&드롭해줍니다.
공격 시 이펙트를 위한 Trail Renderer 컴포넌트를 추가해 설정해줍니다.
public void Use()
{
if(type == Type.Melee)
{
StopCoroutine("Swing");
StartCoroutine("Swing");
}
}
IEnumerator Swing()
{
//1
yield return new WaitForSeconds(0.1f);
meleeArea.enabled = true;
trailEffect.enabled = true;
//2
yield return new WaitForSeconds(0.3f);
meleeArea.enabled = false;
//3
yield return new WaitForSeconds(0.3f);
trailEffect.enabled = false;
}
//Use() 메인루틴 -> Swing() 서브루틴 -> Use() 메인루틴
//Use() 메인루틴 + Swing() 코루틴 (Co-Op)
}
스크립트를 작성합니다.
애니메이터에서 Swing 애니메이션과 트리거를 작성합니다.
Swing애니메이션을 실행하며 Trail Renderer 컴포넌트를 수정해줍니다.
public class Weapon : MonoBehaviour
{
public enum Type { Melee, Range };
public Type type;
public int damage;
public float rate;
public BoxCollider meleeArea;
public TrailRenderer trailEffect;
public void Use()
{
if(type == Type.Melee)
{
StopCoroutine("Swing");
StartCoroutine("Swing");
}
}
IEnumerator Swing()
{
//1
yield return new WaitForSeconds(0.1f);
meleeArea.enabled = true;
trailEffect.enabled = true;
//2
yield return new WaitForSeconds(0.3f);
meleeArea.enabled = false;
//3
yield return new WaitForSeconds(0.3f);
trailEffect.enabled = false;
}
//Use() 메인루틴 -> Swing() 서브루틴 -> Use() 메인루틴
//Use() 메인루틴 + Swing() 코루틴 (Co-Op)
}
Weapon 스크립트를 수정해줍니다.
void Attack()
{
if (equipWeapon == null) //무기가 있을때만 실행되도록 장비체크
return;
fireDelay += Time.deltaTime; //공격딜레이에 시간을 더해줌
isFireReady = equipWeapon.rate < fireDelay; //공격가능 여부 확인
if(fDown && isFireReady && !isDodge && !isSwap)
{
equipWeapon.Use(); //조건 충족시 Use 실행
anim.SetTrigger("doSwing");
fireDelay = 0; //공격딜레이
}
}
Player스크립트에 Attack();를 추가해줍니다.
게임 뷰에서 정상적으로 작동합니다.
728x90
반응형