简体   繁体   English

Vector3 正在重置为 0,0,0 Unity

[英]Vector3 is being reset to 0,0,0 Unity

I'm making a game right now where the enemies become alerted to the player and chase them indefinetely until the player stand on a "safe plate" in game.我现在正在制作一个游戏,敌人会向玩家发出警报并无限期地追逐他们,直到玩家站在游戏中的“安全盘子”上。 When the player stands here the enemies should then return to their original position.当玩家站在这里时,敌人应该回到他们原来的 position。

The issue is that whenever the player stands on the plate I get a null reference exception error and I can see the original guard position has been reset to 0,0,0 in the console.问题是每当玩家站在盘子上时,我都会收到 null 参考异常错误,并且我可以看到原始保护 position 在控制台中已重置为 0,0,0。 I'm assuming the reason for the null reference exception is because the world origin isn't on the nav mesh I'm using, although I could easily be wrong about that.我假设 null 参考异常的原因是因为世界原点不在我正在使用的导航网格上,尽管我很容易错了。 I just can't seem to figure out why the vector3 value has changed at all since the guardPosition variable is initiated on Start and never touched again.我似乎无法弄清楚为什么 vector3 值发生了根本变化,因为 guardPosition 变量在 Start 上启动并且再也没有被触及过。

I've included both my enemyAi class (script attached to enemy) and my class associated with stepping on plates.我已经包括了我的enemyAi class (附加到敌人的脚本)和我的class 与踩板相关。 If there's anything more needed to include let me know.如果还有什么需要包括的,请告诉我。 If anyone has any ideas, help would be appreciated.如果有人有任何想法,我们将不胜感激。 Cheers.干杯。

Screenshot on console after stepping on plate上板后控制台截图

public class EnemyAI : MonoBehaviour
{
    
    DeathHandler dh;

    [SerializeField] Transform target;
    [SerializeField] float chaseRange = 5f;
    [SerializeField] float killRange = 2f;

    [SerializeField] GameObject explosionEffect;

    NavMeshAgent navMeshAgent;
    float distanceToTarget = Mathf.Infinity;
    bool isDead = false;
    bool isAlert = false;

    Vector3 guardPosition;

    void Start()
    {
        GameObject gob;
        gob = GameObject.Find("Player");
        dh = gob.GetComponent<DeathHandler>();

        guardPosition = transform.position;

        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        distanceToTarget = Vector3.Distance(target.position, transform.position);

        print(guardPosition + " guard position during update");

        //alert a dude
        if (distanceToTarget <= chaseRange && !isDead)
        {
            isAlert = true;
        }

        //chase a dude
        if (isAlert == true)
        {
            ChasePlayer();
            print(isAlert);
        }
    }

    public void ChasePlayer()
    {
        navMeshAgent.SetDestination(target.position);

        //explode a dude
        if (distanceToTarget <= killRange)
        {
            Explode();
            dh.KillPlayer();
        }
    }

    public void SetAlertStatus(bool status)
    {
        isAlert = status;
    }

    public void ReturnToPost()
    {
        //isAlert = false;
        print(guardPosition + " guard position after stepping on plate");
        navMeshAgent.SetDestination(guardPosition);
    }    

    void Explode()
    {
        Instantiate(explosionEffect, transform.position, transform.rotation);

        isDead = true;
        Destroy(gameObject);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, chaseRange);
    }
}
public class SafeSpots : MonoBehaviour
{
    
EnemyAI enemyAi;

    

    void Start()
    {
        GameObject gob;
        gob = GameObject.FindGameObjectWithTag("Enemy");
        enemyAi = gob.GetComponent<EnemyAI>();
    }

    public void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            

            enemyAi.SetAlertStatus(false);
            enemyAi.ReturnToPost();
        }
    }
}

You are getting a disabled instance of EnemyAI .您将获得EnemyAI的禁用实例。 Instead, use FindGameObjectsWithTag then iterate through all of them and add their EnemyAI to a list which you can iterate through when necessary.相反,使用FindGameObjectsWithTag然后遍历所有它们并将它们的EnemyAI添加到一个列表中,您可以在必要时对其进行迭代。 By the way, it's better to use CompareTag when possible to reduce garbage:顺便说一句,最好尽可能使用CompareTag来减少垃圾:

public class SafeSpots : MonoBehaviour
{
    
    List<EnemyAI> enemyAis;

    void Start()
    {
        GameObject[] enemies= GameObject.FindGameObjectsWithTag("Enemy");
        enemyAis = new List<EnemyAI>();
       
        foreach (GameObject enemy in enemies)
        {
            enemyAis.Add(enemy.GetComponent<EnemyAI>());
        }
    }

    public void OnTriggerStay(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            foreach(EnemyAI enemyAi in enemyAis)
            {
                enemyAi.SetAlertStatus(false);
                enemyAi.ReturnToPost();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM