ํ์ฌ ์ํฉ
ํ๋ ์ด์ด๊ฐ ์์น๋ฅผ ์ด๋ํ ํ ์ผ์ ๋ฒ์ ๋ด์ ์ ๋ค์ ๋๋ฐฑ์ด ๋ค๋ก ๋๋ ๊ฒ์ด ์๋๋ผ ํ๋ ์ด์ด์ชฝ์ผ๋ก ๋จ.
์ผ์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๊ณณ์ ์ ๋ค์ ์ ์์ ์ผ๋ก ํ๋ ์ด์ด๊ฐ ๊ณต๊ฒฉํ ๋ฐฉํฅ์ผ๋ก ๋๋ฐฑ๋จ.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy1RE : MonoBehaviour
{
Transform player;
public bool isFlipped = false;
public bool isHurted;
SpriteRenderer rend1;
Rigidbody2D rigid2d;
Animator anim;
BoxCollider2D box2denemy1;
public int enemy1Hp = 5; // ์ฒด๋ ฅ
float f_en1KnockBackBound = 7.0f;
public float eneDelay; // ๊ณต๊ฒฉ ์ฟจํ์
public float punchDelay = 1.5f; // ํ์น ๋๋ ์ด
void Start()
{
rend1 = GetComponent<SpriteRenderer>();
rigid2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
box2denemy1 = GetComponent<BoxCollider2D>();
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
if(eneDelay >= 0)
{
eneDelay -= Time.deltaTime;
}
if(enemy1Hp <=2)
{
punchDelay = 3.0f;
f_en1KnockBackBound = 8.0f;
}
}
public void LookAtPlayer()
{
Vector3 flipped = transform.localScale;
flipped.z *= -1f;
if(transform.position.x > player.position.x && isFlipped)
{
transform.localScale = flipped;
transform.Rotate(0, 180f, 0);
isFlipped = false;
}
else if(transform.position.x < player.position.x && !isFlipped)
{
transform.localScale = flipped;
transform.Rotate(0, 180f, 0);
isFlipped = true;
}
}
public void TakeDamageEnemy1(int damage, Vector2 pos)
{
if (!isHurted)
{
isHurted = true;
enemy1Hp -= damage;
if (enemy1Hp <= 0)
{
StartCoroutine(Enemy1Died());
}
else
{
float x1 = transform.position.x - pos.x; // ๋๋ฆฐ ์ ์ ๋ฐฉํฅ
if (x1 < 0) x1 = 1;
else x1 = -1;
StartCoroutine("HurtCheckEnemy1");
StartCoroutine("ChangeHurtColorenemy1");
StartCoroutine("KnockBack", (x1));
}
}
}
IEnumerator HurtCheckEnemy1()
{
yield return new WaitForSeconds(0.1f);
isHurted = false;
}
IEnumerator ChangeHurtColorenemy1()
{
while (isHurted)
{
anim.SetTrigger("En1hurted");
yield return new WaitForSeconds(0.5f);
}
}
IEnumerator KnockBack(float dir)
{
float f_KnockbackTime = 0;
while (f_KnockbackTime < 0.2f)
{
if (transform.rotation.y == 0)
{
transform.Translate(Vector2.left * f_en1KnockBackBound * Time.deltaTime * dir);
}
else transform.Translate(Vector2.left * f_en1KnockBackBound * Time.deltaTime * -dir);
f_KnockbackTime += Time.deltaTime;
yield return null;
}
}
IEnumerator Enemy1Died()
{
box2denemy1.isTrigger = true;
rigid2d.isKinematic = true;
anim.SetTrigger("EN1died");
yield return new WaitForSeconds(1.1f);
DestroyImmediate(gameObject);
}
}
์ข์ธก์ ๋ฌธ์ผ๋ก ํ๋ ์ด์ด๊ฐ ์ด๋ํ๊ณ ์ฐ์ธก ๋ํธ๋จธ๋ฆฌ์ ์ ์ด ์์๋ ๊ณณ ์๊น์ง์ ๋ชจ๋ ์ ๋ค์ ์ฃ๋ค ๋๋ฐฑ ๋ฐฉํฅ์ด ๋ฐ๋๋ก ์ฒ๋ฆฌ๋จ.
--
ํด๊ฒฐ ์ด๋ผ๊ณ ํด๋ ๋ ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ์์ ํด๊ฒฐ๋ก ํจ
ํ๋ ์ด์ด์ ๋ฐฉํฅ์ ๊ฒฐ์ ์ง๋ float x1 ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ์ฌ ํด๊ฒฐ
x1 = transform.positon.x - pos.x
๋ฅผ
x1 = transform.localposition.x - pos.x
๋ก
ํ๋ ์ด์ด์ ์์น๊ฐ์ ๋ก์ปฌํฌ์ง์ ์ผ๋ก ๋ฐ๋๊ฒ ๋ง๋ค๊ณ ์๊ฐํ์
๋น์ฅ์ ์๋ฌ๊ฐ ์๋๋ฐ ์ด๊ฒ ๋์ค์ ์ด๋ค ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ง๋ ์ ๋ชจ๋ฅด๊ฒ ์
๋ฐฉํฅ ํ๋จ์ y rotation์ผ๋ก ํ๋ ๋ถ๋ถ์ด ์์งํ ์์ฌ์ค๋ฌ์ด๋ฐ. ์์ isFlipped๊ฐ ์ ๋๋ก ์๋ํ๋๊ฑฐ๋ฉด ๊ทธ๊ฑธ๋ก ํด๋ด
ํ์ฌ๋ ๋๋ฐฑ ์ฝ๋ฃจํด์์ float x1 = transform.position.x - pos.x; // ๋๋ฆฐ ์ ์ ๋ฐฉํฅ if (x1 < 0) x1 = 1; else x1 = -1;
์ ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ด์ค์ ์ฒ๋ฆฌํจ isFlipped๋ ์ฒ๋ค๋ณด๋ ๋ฐฉํฅ์ด๋ผ ์ ๊ฒ ๋ ๊ฑฐ๊ฐ์ง ์์์
์ผ๋จ ๋๋ฐฑ ์ฝ๋ฃจํด์์ rotation y๊ฐ ๋จ์ํ 0์ธ์ง ์๋์ง๋ก ๊ตฌ๋ถํ๋๊ฑด ๋ง์ด ์์ข์๋ณด์. ์๋ฅผ๋ค์ด ์ด๋ค ์ด์ ๋ก 0.0001์ด ๋๋ค๋์ง ํ ์๋ ์๊ฑฐ๋ . ์ด๋ถ๋ถ์ ํ์ธํ๋๊ฒ ์ข์๋ฏ
transform.rotation์ 1๋ณด๋ค ๋ฎ์ ์ ๋๋ก ํ๋ฉด ๋๋ ค๋?