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

Unity 2D敌人AI代码报错:castPoint未定义求助

Unity 2D玩家追踪AI开发问题:castPoint变量未定义

我在跟着教程制作Unity 2D玩家追踪AI时,代码里的castPoint变量提示未定义,无法正常编译。我C#基础比较薄弱,加上有阅读障碍,教程节奏太慢很难集中注意力理解细节,所以附上我的代码,请求技术帮助。

我的代码:

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

public class Presentsaswehear : MonoBehaviour
{
    // Start is called before the first frame update
    //ignore this code!!!
    private Animator Enemtestone;
    public GameObject FirstTrig;
    public static bool Izintyloip;
    private bool Thecheckert;
    //ignore this code!!!


    //Enemy part tracking blablablaaaaa
    [SerializeField]
    public Transform Player;

    [SerializeField]
    float agroRange;

    [SerializeField]
    float moveSpeed;

   private  Rigidbody2D rb2det;  
    //End of ai part

    

    void Start()
    {
       //ignore this code!!!
       Enemtestone = GetComponent<Animator>();
        //FirstTrig.SetActive(true);
        Izintyloip = false;
        
        Thecheckert = false;
        //ignore this code!!!
        
        rb2det = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Izintyloip == true) { 
            Enemtestone.SetInteger("Wakeup", 1);
            Thecheckert = true;
        }
        if (CanSeePlayer(agroRange))
        {

            ChasePlayer();

        }
        else
        {

            StopChasingPlayer();

        }
        
        
        
        
        
        
        
        
        
        /*if (Thecheckert == true)
        {
            float distToPlayer = Vector2.Distance(transform.position, Player.position);
            if (distToPlayer < agroRange)
            {

                ChasePlayer();

            }
            else
            {

                StopChasingPlayer();

            }


        }*/
    }

    
    bool CanSeePlayer(float distance)
    {
        bool val = false;
        float castDist = distance;
        
        Vector2 endPos = castPoint.position + Vector3.right * distance;
        
        RaycastHit2D hit = Physics2D.Linecast(castPoint.position, endPos, 1 << LayerMask.NameToLayer("Action"));
        if(hit.collider != null)
        {

            if (hit.collider.gameObject.CompareTag("Player"))
            {
                val = true;

            }
            else
            {
                val = false;
            }


        }
        return val;
    }

    void ChasePlayer()
    {

        if(transform.position.x < Player.position.x)
        {

            rb2det.velocity = new Vector2(moveSpeed, 0);
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
        else
        {

            rb2det.velocity = new Vector2(-moveSpeed, 0);

            transform.eulerAngles = new Vector3(0, 0, 0);

            // if(transform.position.x > player.postion.x)
        }

        Enemtestone.SetBool("GFW", true);
    }
    void StopChasingPlayer()
    {
        rb2det.velocity = new Vector2(0, 0);

        Enemtestone.SetBool("GFW", false);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 05:30:57