using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CharacterMovement : MonoBehaviour {
public GameObject effect;//์ฌ๋ง ํจ๊ณผ
public int health = 3; //์บ๋ฆญํฐ ์ฒด๋ ฅ
[SerializeField] private float speed;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f; //์ด๋ ์ ๋ถ๋๋ฌ์ ์กฐ์ ์ ์ฌ์ฉ
//[SerializeField] private Animator animator;
private float moveInputHorizontal;
private float moveInputVertical;
private Rigidbody2D rb;
private bool facingRight = true; //์บ๋ฆญํฐ ์คํ๋ผ์ดํธ ๋ค์ง๋ ๋ฐ์ ์ฌ์ฉ
private Vector3 refVelocity = Vector3.zero; //smoothDamp์ ์ฌ์ฉ
private SpriteRenderer spriteRenderer; //์บ๋ฆญํฐ sprite renderer
[SerializeField]
private bool death = false;
void Start () {
rb = GetComponent();
spriteRenderer = GetComponent();
death = false;
}
void FixedUpdate()
{
//์บ๋ฆญํฐ ์ด๋
moveInputHorizontal = Input.GetAxisRaw("Horizontal");
moveInputVertical = Input.GetAxisRaw("Vertical");
Vector3 targetVelocity = new Vector3(moveInputHorizontal * Time.fixedDeltaTime, moveInputVertical * Time.fixedDeltaTime).normalized * speed;
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref refVelocity, m_MovementSmoothing);
//animator.SetFloat("Speed", Mathf.Abs(moveInputHorizontal) * speed); // ์ข์ฐ ์ ๋๋ฉ์ด์
if (facingRight == false && moveInputHorizontal > 0)
{
Flip();
}
else if (facingRight == true && moveInputHorizontal
{
Flip();
}
}
void Update () {
if (health <= 0)
{
if (death == false)
{
Instantiate(effect, transform.position, Quaternion.identity);
//rb.isKinematic = true;
rb.constraints = RigidbodyConstraints2D.FreezePosition;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
rb.velocity = new Vector2(0, 0);
death = true;
Invoke("RestartGame", 2f);
}
}
}
void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//์ข์ฐ ๋ฐฉํฅํค์ ๋ฐ๋ผ ์บ๋ฆญํฐ ๋ค์ง๊ธฐ
void Flip()
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
์๊น ์ง๋ฌธ์ ์ฌ๋ ธ๋๋ฐ, ์ฝ๋ ์ ์ฒด๋ฅผ ์ฌ๋ ค์ผ ํ์คํ ๋ต๋ณ์ด ๋ฌ๋ฆด ๊ฒ ๊ฐ์์ ์ ์ฒด ์ฌ๋ ค์ผ!
์ด๋ค ๋ถ์ด ์์ฒญ ์น์ ํ๊ฒ ์ค๋ช ํด์ฃผ์๋ฉด์ ํ๋กํผํฐ ์ฐ๋ผ๊ณ ๋ง์ํด์ฃผ์ จ๋๋ฐ, ํ๋กํผํฐ ์จ๋ ์คํ์ด ์๋๊ณ ์ ๋ฐ์ดํธ ์์ ๋ฃ์ด๋ ์คํ์ด ์๋์,
์ผ๋จ์ ์ ์๊ฒ ์ต์ํ ์ ๋ฐ์ดํธ๋ฌธ ์์ ๋ฃ์ด์ ํ ์คํธ๋ฅผ ํด๋ณด๊ณ ์๊ธด ํ๋ฐ, ๋์ค์ ์คํ์ด ๋๋ฐ๋ก ๋๋ฉด ํ๋กํผํฐ๋ก ์ฎ๊ฒจ๋ณด๋ ค๊ตฌ์.
์ผ๋จ, ๋ฌธ์ ๋ ์ ๊ธฐ ์ ๋ฐ์ดํธ๋ฌธ ์์ ์๋ ๋ด์ฉ์ ๋๋ค.
์ ์ด๋ ๋ถ๋ซํ ๋๋ง๋ค health๋ ์ ์์ ์ผ๋ก ๊น์ด๊ณ , health๊ฐ 0์ด ๋๋ฉด death๋ ์ ์์ ์ผ๋ก true๊ฐ ๋ฉ๋๋ค.
๊ทผ๋ฐ ์บ๋ฆญํฐ ๋ฉ์ถ๊ฒ ํ ๋ ค๋ฉด
rb.constraints = RigidbodyConstraints2D.FreezePosition;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
rb.velocity = new Vector2(0, 0);
์๋ ๊ฒ ํ๋๊ฑฐ ์๋๊ฐ์? ใ ;
invoke๋ ๋ ์ ์์ ์ผ๋ก ์คํ๋์, health๊ฐ 0์ด ๋๊ณ 2์ด ๊ธฐ๋ค๋ฆฌ๋ฉด ์ฌ์ด ์ด๊ธฐํ๋๋๋ฐ, ์ ๋ถ๋ถ๋ง ์ ์ฉ์ด ์๋๋ค์ฉ
์ ๋ฆฌํ์๋ฉด health๊ฐ 0์ด ๋๊ณ ๋์๋ ์บ๋ฆญํฐ๋ ๊ณ์ ์์ง์ผ ์ ์๊ณ , ๊ทธ๋ ๊ฒ ๋ง ์์ง์ด๋ค๊ฐ 2์ด ์ง๋๋ฉด ์ฌ์ด ์ฌ์์๋ผ์
์นํ์ ์ด๋ผ ์์ง ์ ์ฝ๋์ ๋ฌธ์ ์ ์ด ๋ญ์ง ๋ชจ๋ฅด๊ฒ ๋ค์ผ
์๋ ค์ฃผ์์ผ
๋ณด๋ฉด FixedUpdate์์ rb.velocity๋ฅผ 0,0์ผ๋ก ์ก์์ฃผ๊ณ ๋ฐ๋ก ๋ค์ SmoothDamp๋ก ์ฌ์ค์ ํ๋๋ฐ ์๋ฌธ์ ์๋๊ฐ
๋์ถฉ ๊ทธ ๋ฌธ์ ์ธ๊ฒ๊ฐ์๋ฐ ์บ๋ฆญํฐ ์ด๋ ์ ์ฒด๋ฅผ if(!death) ์์ผ๋ก ๋ฃ์ผ๋ฉด ๋ ๋ฏ
๊ทธ๋ฅ ์์ง์ ๋ฐ์์ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ ์ ์ฒด์ ์ดํ๋ฌธ์ค์ ๋ฐ์ค๊ฐ ํธ๋ฃจ๊ฐ ๋๋ฉด ์๋ ์ํ๊ฒ ํ๋ฉด ๋์๋ ....
์๋ ์ ๊ทธ๋ด ์๊ฐ์ ๋ชปํ์ง...ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ๊ฐ์ฌ๋๋ฆฝ๋๋ค ํ๋๋ค.. ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ง ์ํด๋ด์ ๊ทธ๋ฐ๊ฐ ๋จธ๋ฆฌ๊ฐ ๊ตฌ๋ฐ๊ธฐ๋ผ์ ๊ทธ๋ฐ๊ฐ ๋ฌธ์ ๊ฐ ์๊ธฐ๋ฉด ํด๊ฒฐ์ฑ ์ด ์๊ฐ์ด ์๋๋ค์ .. ๋ค์ ํ ๋ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค์