2d게임에서 캐릭터가 움직이고 대쉬를 하는 코드를 짰습니다 wasd로 움직이고 스페이스바를 누르면 플레이어가 가는 방향으로 대쉬를 하고 대쉬이펙트를 만드는 코드입니다
그러다가 할로우나이트처럼 처음에는 대쉬가 없다가 후반에 조건을달성해야 얻어야 사용할 수 있으면 더 재밌을것 같아서 코드를 둘로 나누었는데 움직이는건 잘 작동하는데 스페이스바를 눌러도 아무일도 일어나지 않습니다
몇시간동안 해봐도 해결이 안되는데 아직 초보여서 기능을 다 모르는데 혹시 코드에 문제가 있거나 코드를 나누지 않아도 해결할 수 있는 방법이 있을까요
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
private Vector3 moveDelta;
private float xInput;
private float yInput;
public float speed;
private bool isDashing = false;
public float dashSpeed;
public float startDashTime;
private float dashTime;
private float nextDashTime = 0f;
public float dashCooldown;
public Animator animator;
public GameObject dashEffectPrefab;
public float dashEffectDuration;
private void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
transform.localScale = new Vector3(0.7f, 0.7f, 1f);
dashTime = startDashTime;
}
private void Update()
{
HandleMovement();
HandleDashing();
}
private void FixedUpdate()
{
if (isDashing && dashTime > 0)
{
dashTime -= Time.fixedDeltaTime;
}
else if (Mathf.Approximately(dashTime, 0))
{
isDashing = false;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = moveDelta * speed;
}
}
private void HandleMovement()
{
// Handle regular movement
xInput = Input.GetAxisRaw("Horizontal");
yInput = Input.GetAxisRaw("Vertical");
moveDelta = new Vector3(xInput, yInput, 0).normalized;
if (Mathf.Abs(xInput) > 0 || Mathf.Abs(yInput) > 0)
{
animator.SetBool("running", true);
transform.localScale = new Vector3(moveDelta.x > 0 ? -0.7f : 0.7f, 0.7f, 1f);
}
else
{
animator.SetBool("running", false);
}
}
private void HandleDashing()
{
if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextDashTime)
{
isDashing = true;
nextDashTime = Time.time + dashCooldown;
if (Mathf.Abs(xInput) > 0 || Mathf.Abs(yInput) > 0)
{
rb.velocity = moveDelta * dashSpeed;
Vector3 direction = new Vector3(xInput, yInput, 0).normalized;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// Instantiate the dash effect prefab
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, rotation);
Destroy(dashEffect, dashEffectDuration);
}
else
{
rb.velocity = transform.right * dashSpeed;
// Instantiate the dash effect prefab
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, transform.rotation);
Destroy(dashEffect, dashEffectDuration);
}
}
}
}
라는 코드를 이동하는 코드와 대쉬를 담당하는 코드 두개로 나누었습니다
using UnityEngine;
public class PlayerDashing : MonoBehaviour
{
private Rigidbody2D rb;
private Vector3 moveDelta;
private float xInput;
private float yInput;
public float speed;
private bool isDashing = false;
public float dashSpeed;
public float startDashTime;
private float dashTime;
private float nextDashTime = 0f;
public float dashCooldown;
public Animator animator;
public GameObject dashEffectPrefab;
public float dashEffectDuration;
private void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
transform.localScale = new Vector3(0.7f, 0.7f, 1f);
dashTime = startDashTime;
}
private void Update()
{
HandleDashing();
}
private void FixedUpdate()
{
if (isDashing && dashTime > 0)
{
dashTime -= Time.fixedDeltaTime;
}
else if (Mathf.Approximately(dashTime, 0))
{
isDashing = false;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
}
else
{
rb.velocity = moveDelta * speed;
}
}
private void HandleDashing()
{
if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextDashTime)
{
isDashing = true;
nextDashTime = Time.time + dashCooldown;
if (Mathf.Abs(xInput) > 0 || Mathf.Abs(yInput) > 0)
{
rb.velocity = moveDelta * dashSpeed;
Vector3 direction = new Vector3(xInput, yInput, 0).normalized;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
// Instantiate the dash effect prefab
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, rotation);
Destroy(dashEffect, dashEffectDuration);
}
else
{
rb.velocity = transform.right * dashSpeed;
// Instantiate the dash effect prefab
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, transform.rotation);
Destroy(dashEffect, dashEffectDuration);
}
}
}
}
이런 문제는 HandleDashing 함수에 Debug.Log로 코드 실행 전 변수를 일일이 찍어서 확인해보는게 마음 편함 - dc App
상태머신 패턴으로 캐릭터의 상태랑 전이로 나눠서 각잡고 만드는게 아닌이상, 기능별로 저렇게 여러개 스크립트 나눠 작업하는건 초보입장에선 헷갈리기만 할거같아요. 동작 안하는 코드구간에 디버그 로그 찍어봐서 로그 찍히는지 관찰 ㄱㄱ
상태머신패턴이라는게 FSM? 이걸 말하는건가요?
넵
어디서부터 작동안되는건지 체크하는 습관 필수임. 인스펙터에 dashCooldown 이랑 dashTime Time.time 띄어서 변화가 있는지 부터 체크해보셈