Я пытался дать NPC игры, над которой я работаю, над способностью «чувствовать», когда Игрок находится рядом. Я создал этот скрипт, и по неизвестным причинам bool "found" остается false, а когда я автоматически устанавливаю его в true, он возвращается в false (он все равно отправляет позицию игрока в скрипт goTo, так что, по крайней мере, работает). Кто-нибудь знает, как решить эту проблему?
public class NPCLookForPlayerScript : MonoBehaviour {
public bool found; //player found
public float awareness; //how large is the circlecast
public int keepLooking; //for how much time, after losing sight of the player, he tries to keep on looking for him
public GameObject player; //variable to lock on to the player
int timer; //variable to decrement while player isn't in line of sight
NPCGoToScript goTo;
// Use this for initialization
void Start () {
goTo = GetComponent<NPCGoToScript>();
}
// Update is called once per frame
void Update () {
//he's always looking for the player
Collider2D coll = Physics2D.OverlapCircle((Vector2)transform.position, awareness);
//if the player is found, keep looking for him
if (coll.gameObject == player)
{
found = true;
timer = keepLooking;
}
//if the player was found,
if (found)
{
timer = timer - 1; //less time to look for the player
goTo.newPosition(player.transform.position);
}
//if the player is out of sight for too much time, stop looking for him
if (timer <= 0)
{
found = false;
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere((Vector2)transform.position, awareness);
}
}
Поскольку кто-то заставил меня заметить, что значения переменных не записаны в коде, позвольте мне прояснить: я работаю над единством, у этого скрипта есть c значения, которые могут быть изменены инспектором, что очень полезно, так как этот скрипт должен использоваться для разных типов NPC. Значит, значения не равны нулю. осведомленность = 5f и keepLooking = 20. На игровом поле есть игрок GameObject.