
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
즉문즉답 다른 내용에서 OnTriggerEnter2D 부분과 Update 부분을 신경 쓰라고 해서 여러가지로 변경 해보았는데
아직 제가 방법을 잘 모르는 것 같았습니다 ㅎㅎ
한번 알려주시면 감사하겠습니다!
작성한 코드 및 에러 메세지
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cat : MonoBehaviour
{
float full = 5f;
float energy = 0f;
bool isFull = false;
public int type; // 0=normalCat 1=fatCat 2=pirateCat
// Start is called before the first frame update
void Start()
{
float x = UnityEngine.Random.Range(-8.5f, 8.5f);
float y = 30f;
transform.position = new Vector3(x, y, 0);
if (type == 1)
{
full = 10f;
}
if (type == 2)
{
full = 6f;
}
}
// Update is called once per frame
void Update()
{
if (energy < full)
{
if (type == 0)
{
transform.position += new Vector3(0, -0.05f, 0);
}
else if (type == 1)
{
transform.position += new Vector3(0, -0.03f, 0);
}
else if (type == 2)
{
transform.position += new Vector3(0, -0.1f, 0);
}
if (transform.position.y < -16.0f)
{
// 게임오버!
gameManager.I.gameOver();
}
}
else
{
if (transform.position.x > 0)
{
transform.position += new Vector3(0.05f, 0.0f, 0.0f);
}
else
{
transform.position += new Vector3(-0.05f, 0.0f, 0.0f);
}
Destroy(gameObject, 3f);
}
}
void OnTriggerEnter2D(Collider2D coll) //문제 : energy full 일시 한번 더 맞아야 score 오름
{
if (coll.gameObject.tag == "food")
{
if (energy < full)
{
energy += 1.0f;
Destroy(coll.gameObject);
gameObject.transform.Find("hungry/Canvas/front").transform.localScale = new Vector3(energy / full, 1.0f, 1.0f);
}
else
{
if (isFull == false)
{
gameManager.I.addCat();
gameObject.transform.Find("hungry").gameObject.SetActive(false);
gameObject.transform.Find("full").gameObject.SetActive(true);
isFull = true;
}
}
}
}
}
