簡體   English   中英

代碼不調用其他關於統一的 else 條件語句

[英]code do not called the other else conditional statment on unity

我統一創建了一個程序,它使用一個變換數組作為 Ai 的位置,一旦完成,它將點對點移動,它將再次從零重新開始。

我還制作了另一個發射光線投射的程序,當它檢測到敵人時,它將停止巡邏程序並啟用導航網格代理,否則它將打開巡邏腳本並關閉導航網格代理,但最后一部分不起作用出於某種原因,我需要幫助知道它為什么不起作用

這是巡邏隊的代碼

    public Transform[] Points;
    public int current;

    public NavMeshAgent agent;

    public milkscript milk;
    public float speed;
    
    // Start is called before the first frame update
    void Start()
    {
        current = 0;

        agent.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {

        if(milk.chasing == false) 
        {
            agent.enabled = false;

            Patol();
        }
        if(milk.chasing == true) 
        {
            agent.enabled = true;
        }
    }

    public void Patol() 
    {
          if(transform.position != Points[current].position) 
        {
            transform.position = Vector3.MoveTowards(transform.position,Points[current].position, speed * Time.deltaTime);
        }
        else 
        {
            current = current+1 % Points.Length;
        }

        if(current == 8) 
        {
            current = 0;
        }
    }

這里是光線投射的代碼

    NavMeshAgent Agent;

    public Patrol patrol;

    public bool chasing;


    public Transform enemy;

    public Transform[] Points;

    [SerializeField] LayerMask layermask;

    public Transform Point;

    public float raydistance = 10f;

    public float enemyview = 5f;
 
    // Start is called before the first frame update
    void Start()
    {
        Agent = GetComponent<NavMeshAgent>();
         chasing = false;
    }

    // Update is called once per frame
    void Update()
    {
            if(Physics.Raycast(Point.position, Point.forward, out var Hit, raydistance, layermask)) 
        {
            Debug.DrawLine(Point.position, Hit.transform.position, Color.red);
            
            Debug.Log(Hit.transform.name);
        
            if(Hit.transform.name == "Chocltaemilk") 
            {
                chasing = true;
                patrol.enabled = true;
                Agent.SetDestination(Hit.transform.position);
            }
        }
        else 
        {
            Agent.enabled = false;
            Debug.DrawRay(Point.position, Point.forward, Color.green);
            patrol.Patol();
        }

        if(chasing == false) 
        {   
            Agent.enabled = false;
            patrol.enabled = true;
        }
         
        if(chasing == true) 
        {   
            Agent.enabled = true;
            patrol.enabled = false;
        }
    }

    void returen() 
    {
        chasing = false;
        patrol.enabled = false;
    }

    public void ChasePlayer(Transform target)
    {
        Agent.SetDestination(target.position);
        transform.LookAt(target.position);
    }

這可能意味着您總是遇到以下問題:
Physics.Raycast(Point.position, Point.forward, out var Hit, raydistance, layermask)

當你碰到其他東西時發生 else ,你可以嘗試重新排列你的括號{}像這樣:

void Update()
{
    if(Physics.Raycast(Point.position, Point.forward, out var Hit, raydistance, layermask)) 
    {
        Debug.DrawLine(Point.position, Hit.transform.position, Color.red);
        
        Debug.Log(Hit.transform.name);
    
        if(Hit.transform.name == "Chocltaemilk") 
        {
            chasing = true;
            patrol.enabled = true;
            Agent.SetDestination(Hit.transform.position);
        } else {
            Agent.enabled = false;
            Debug.DrawRay(Point.position, Point.forward, Color.green);
            patrol.Patol();
        }
    }

    if(chasing == false) 
    {   
        Agent.enabled = false;
        patrol.enabled = true;
    }
     
    if(chasing == true) 
    {   
        Agent.enabled = true;
        patrol.enabled = false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM