
고양이가 죽기도 전에 옆으로 이동하는데
안 죽었을때 내려오는 조건문도 같이 발동이 되어서 대각선으로 내려오면서 화면 밖으로 나갑니다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cat : MonoBehaviour
{
public RectTransform front;
public GameObject HungryCat;
public GameObject FullCat;
float full = 5.0f;
float enegry = 0.0f;
// Start is called before the first frame update
void Start()
{
float x = Random.Range(-9.0f, 9.0f);
float y = 30.0f;
transform.position = new Vector2(x, y);
}
// Update is called once per frame
void Update()
{
if (transform.position.x < -10.0f)
{
Destroy(gameObject);
}
if (transform.position.x > 10.0f)
{
Destroy(gameObject);
}
if (enegry < full)
{
transform.position += Vector3.down * 0.05f;
if (transform.position.y < -16.0f)
{
GameManager.Instance.GameOver();
}
else
{
if (transform.position.x > 0)
{
transform.position += Vector3.right * 0.05f;
}
else
{
transform.position += Vector3.left * 0.05f;
}
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Food"))
{
if (enegry < full)
{
enegry += 1.0f;
front.localScale = new Vector3(enegry / full, 1.0f, 1.0f);
Destroy(collision.gameObject);
}
if (enegry == 5.0f)
{
HungryCat.SetActive(false);
FullCat.SetActive(true);
}
}
}
}
