You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity修改Astar的AIDestinationSetter遇类型找不到错误及解决

问题详情

尝试修改Astar路径插件的AIDestinationSetter脚本,实现玩家关闭手电筒时敌人停止追踪的功能,但开发中遇到编译错误:

  • 报错“无法找到类型或命名空间‘Flashlight’”
  • 初始阶段还出现“无法找到类型或命名空间‘FlashlightSwitch’”的错误

尝试过以下修复手段均未解决:

  • 为自定义脚本添加命名空间
  • 创建并使用程序集定义文件夹
  • 将自定义脚本移至Astar插件的Behaviors目录下

重启笔记本后,所有编译错误消失,功能恢复正常,暂未明确具体触发原因。

涉及脚本代码

FlashlightSwitch 脚本

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

public class FlashlightSwitch : MonoBehaviour
{
    [SerializeField] private GameObject Flashlight;

    [SerializeField] private bool lightOn = true;

    [SerializeField] private float flashRate;

    public bool blobbySees;

    private float nextFlash;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ToggleLight();
        }
    }

    void ToggleLight()
    {
        if (Time.time > nextFlash && lightOn == false)
        {
            nextFlash = Time.time + flashRate;

            Flashlight.SetActive(true);
            lightOn = true;
            print("Flashlight on");
            blobbySees = true;
        }

        if (Time.time > nextFlash && lightOn == true)
        {
            nextFlash = Time.time + flashRate;

            Flashlight.SetActive(false);
            lightOn = false;
            print("Flashlight off");
            blobbySees = false;
        }
    }
}

修改后的 AIDestinationSetter 脚本

using UnityEngine;
using System.Collections;

namespace Pathfinding {

    /// <summary>
    /// Sets the destination of an AI to the position of a specified object.
    /// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
    /// This component will then make the AI move towards the <see cref="target"/> set on this component.
    ///
    /// See: <see cref="Pathfinding.IAstarAI.destination"/>
    ///
    /// [Open online documentation to see images]
    /// </summary>
    [UniqueComponent(tag = "ai.destination")]
    public class AIDestinationSetter : VersionedMonoBehaviour {
        /// <summary>The object that the AI should move to</summary>
        public FlashlightSwitch flashlightSwitch;
        public Transform target;
        public Transform self;
        public Transform player;

        IAstarAI ai;

        void Start()
        {

        }

        void OnEnable () {
            ai = GetComponent<IAstarAI>();
            // Update the destination right before searching for a path as well.
            // This is enough in theory, but this script will also update the destination every
            // frame as the destination is used for debugging and may be used for other things by other
            // scripts as well. So it makes sense that it is up to date every frame.
            if (ai != null) ai.onSearchPath += Update;
        }

        void OnDisable () {
            if (ai != null) ai.onSearchPath -= Update;
        }

        void OnTriggerStay2D(Collider2D other)
        {
            if (other.CompareTag("Player"))
            {
                target = player;
            }
        }

        void OnTriggerExit2D(Collider2D other)
        {
            if (other.CompareTag("Player"))
            {
                target = self;
            }
        }

        /// <summary>Updates the AI's destination every frame</summary>
        void Update () {
            if (target != null && ai != null) ai.destination = target.position;
        }
    }
}

内容的提问来源于stack exchange,提问作者Avva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:37:05