weixin_63967673 2022-08-31 16:34 采纳率: 94.1%
浏览 36
已结题

unity简单怪兽AI报错

报错Assets\Scenes\GuaiWu.cs(23,9): error CS0103: The name 'Wol' does not exist in the current context


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GuaiWu : MonoBehaviour
{
    public enum EnemyState
    {
        idle,
        run,
        attack,
        death,
        
    }
    public EnemyState CurrentState = EnemyState.idle;
    Animator imator;
    UnityEngine.AI.NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
    {
        imator = GetComponent<Animator>();
        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        Wol = GameObject.FindWithTag("Wolf").transform;
    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(Wol.position, transform.position);
        switch (CurrentState)
        {
            case EnemyState.idle:
                if (distance > 2 && distance <= 10)
                {
                    CurrentState = EnemyState.run;
                }
                if (distance < 2)
                {
                    CurrentState = EnemyState.attack;
                }
                imator.Play("Idle");
                agent.isStopped = true;
                break;
            case EnemyState.run:
                if (distance > 10)
                {
                    CurrentState = EnemyState.idle;
                }
                if (distance < 2)
                {
                    CurrentState = EnemyState.attack;
                }
                imator.Play("Run");
                agent.isStopped = false;
                agent.SetDestination(Wol.position);
                break;
        }
    }
}

  • 写回答

2条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-08-31 20:54
    关注

    在第18行后面添加个定义就可以了,代码如下:

    Transform Wol;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月9日
  • 已采纳回答 9月1日
  • 创建了问题 8月31日