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()
{
// 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);
}
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);
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, rotation);
Destroy(dashEffect, dashEffectDuration);
}
else
{
rb.velocity = transform.right * dashSpeed;
GameObject dashEffect = Instantiate(dashEffectPrefab, transform.position, transform.rotation);
Destroy(dashEffect, dashEffectDuration);
}
}
}
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;
}
}
}
라는 탑다운 2d에서 wasd랑 방향키로 움직이고 스페이스바를 누르면 대쉬를 하는 코드인데
대쉬할때 너무 심심해서 대쉬이펙트를 그리고 프리팹으로 만들고 instantiate했는데 어색해서 대쉬 이펙트의 크기가 변하는 애니메이션을 만들었습니다 하지만 애니메이션을 만들때의 위치에 계속 instantiate가 됩니다.. 어떻게 해결해야할까요 지피티랑 1시간 동안 말싸움했는데 계솎 해결 되지 않습니다
해당 댓글은 삭제되었습니다.
와 진짜진짜 감사합니다 ㅠㅠㅠ