using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoliderL : MonoBehaviour
{
Animator animator; // BS์ ์ ๋๋ฉ์ดํฐ ์ปดํฌ๋ํธ
public bool isAttacking = false; // ๊ณต๊ฒฉ ์ค์ธ์ง ํ์ธํ๋ ํ๋๊ทธ
Rigidbody2D rb;
public float speed = 1.5f;
public int health;
bool isDamage; // ๋ฌด์ ํ์ ์ฒ๋ฆฌ
void Start()
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>(); // Animator ์ปดํฌ๋ํธ ๊ฐ์ ธ์ค๊ธฐ
}
void OnHit()
{
// Health Down
health--;
animator.SetTrigger("doHit");
Invoke("ReturnSprite", 0.2f);
if (health <= 0)
{
animator.SetTrigger("doDie");
Destroy(gameObject, 2f);
}
}
void ReturnSprite()
{
animator.SetTrigger("doStop"); // ์ ๋๋ฉ์ด์
์ด ๋ฉ์ถ๋ ๋ฉ์๋
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("RedMelee"))
{
if (!isDamage)
{
//Bullet bullet = other.gameObject.GetComponent<Bullet>();
health -- ;
StartCoroutine(OnDamage());
OnHit();
Debug.Log("์ผ์ชฝ์ ์๋ ๋ธ๋ฃจํ ๋ณ์ฌ๊ฐ ๊ณต๊ฒฉ์ ๋ฐ์์ต๋๋ค.");
}
}
}
IEnumerator OnDamage()
{
isDamage = true;
// ํผ๊ฒฉ ์ด๋ฒคํธ ์๋ต
yield return new WaitForSeconds(1f);
isDamage = false;
// ํผ๊ฒฉ ์ด๋ฒคํธ์์ ๋์์ค๋๊ฑฐ ์๋ต
}
void Update()
{
// BS๋ฅผ ์ผ์ชฝ์ผ๋ก ์ด๋์ํค๊ธฐ
MoveLeft();
}
void MoveLeft()
{
// ํ์ฌ ์๋๋ฅผ ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ์ค์
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
if (!isAttacking) // ์ด๋ฏธ ๊ณต๊ฒฉ ์ค์ด ์๋๋ฉด ๊ณต๊ฒฉ ์์
{
isAttacking = true;
StartCoroutine(AttackRoutine()); // Coroutine ์์
}
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
StopAttack(); // ๊ณต๊ฒฉ ์ค์ง
}
}
IEnumerator AttackRoutine()
{
while (isAttacking)
{
AttackDa();
yield return new WaitForSeconds(1f);
}
}
void AttackDa()
{
animator.SetTrigger("doAttack");
}
void StopAttack()
{
isAttacking = false; // ๊ณต๊ฒฉ ์ค์ง
animator.ResetTrigger("doAttack"); // ์ ๋๋ฉ์ด์
ํธ๋ฆฌ๊ฑฐ ๋ฆฌ์
}
}
์ด๊ฑฐ ์ 1์ด์ ํ๋ฒ์ฉ ๋๋ ค์ง๋๊ฒ ์๋๋ผ
1์ด์ ์ฌ๋ฌ๋ฒ์ฉ ๋๋ ค์ง๋๊ฑฐ์?
1์ด์ 1๋ฒ์ฉ ๋๋ ค์ง๊ฒ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํจ? ์ ๋ฐ ๋์์ฃผ์
๊ฑ ์ ๋ฐ์ดํธ๋ฌธ์ ์ฐ๋๊ฒ ์ฌ์ ์ํ ๋ฐ..์ฝ๋ฃจํด์ ์บ์ฑํด์ ์จ๋ณด์ void OnCollisionEnter2D(Collision2D collision) if (collision.gameObject.CompareTag("Enemy")) { if (!isAttacking) { isAttacking = true; attackCoroutine = StartCoroutine(AttackRoutine()); } }void StopAttack()if (attackCoroutine != null){ StopCoroutine(att
void StopAttack() { if (attackCoroutine != null) { StopCoroutine(attackCoroutine); attackCoroutine = null; } isAttacking = false; animator.ResetTrigger("doAttack"); }
void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Enemy")) { if (!isAttacking) { isAttacking = true; attackCoroutine = StartCoroutine(AttackRoutine()); } } }
๋๊บผ ์ด๊ฑฐ ๋งํ๋๊ฑฐ์๋?
https://gall.dcinside.com/mgallery/board/view/?id=game_dev&no=167300&page=1
์ฝ๋ฃจํด์ด OnCollisionEnter2D ์คํ๋ ๋๋ง๋ค ์์ฑ๋ผ์ ๊ทธ๋ฌ๋ ๋ฏ isAttacking ์ผ๋ก ์ปจํธ๋กคํ ๊ฑฐ๋ฉด Start๋ Awake์์ ํ๋ฒ๋ง ์ฝ๋ฃจํด๋ง๋ค๊ณ OnCollisionEnter2D์์๋ isAttacking๋ง true๋ก ๋ฐ๊ฟ์ผ๋จ
void Awake() { StartCoroutine(AttackRoutine()); }
IEnumerator AttackRoutine() { while (isAttacking) { AttackDa(); yield return new WaitForSeconds(1f); } }
void AttackDa() { animator.SetTrigger("doAttack"); }
์ด๋ฐ์์ผ๋ก ํ๋ฉด ๋๋๊ฑฐ์?
๋ญ์ ์ ๋ ํจ์ฑ๋นํจ
์ฝ๋ฃจํด์ ์ํํ์ผ๋ฉด ์ข ๋ฃ๋ฅผ ์์ผ์ผ์ง
์ด์ํ๋ค ๋๊บผ ๋ฐ๋ผํ๋๋ฐ ๋๋๋ฐ?