首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在更新()统一c#中更改条件

如何在更新()统一c#中更改条件
EN

Stack Overflow用户
提问于 2018-10-29 09:14:55
回答 2查看 468关注 0票数 1

我已经创建了navMeshand代理。对于目标,我使用了两个空对象。对于每个空对象,我创建了两个按钮。

如果我单击白色按钮,代理首先移动到空目标,然后再单击红色按钮代理移动到第二个空目标。

当我想要将代理从target-2移动到target-1时,我将面临问题。我怎么能把那个探员转移到taeget-1?

为了更好的理解,请看视频

视频链路https://youtu.be/zRKHdMeQsi0

代码

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SampleAgentScript : MonoBehaviour {

    public Transform target , target2;
    NavMeshAgent agent;
    private static bool start1=false , start2=false;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    public static void buttonClick()
    {
        //if white button click
        start1 = true;
    }

    public static void buttonClick2()
    {
        //if red button click
        start2 = true;
    }

    void Update()
    {
        if (start1) //if white button click moves to targer-1
        {
            agent.SetDestination(target.position);
        }

        if (start2) //if red button click moves to targer-2
        {
            agent.SetDestination(target2.position);
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-29 09:24:52

也许这会有帮助。

代码语言:javascript
复制
public static void buttonClick()
{
      //if white button click
    start1 = true;
    start2 = false;
}

public static void buttonClick2()
{
     //if red button click
    start2 = true;
    start1 = false;
}
票数 1
EN

Stack Overflow用户

发布于 2018-10-29 09:59:50

您忘记了通过将布尔值重置为false来替代状态。因为您已经在按钮中设置了布尔值,所以您也可以在update函数中替换状态。

代码语言:javascript
复制
void Update()
{
    if (start1) //if white button click moves to targer-1
    {
        agent.SetDestination(target.position);
        start1=false;
    }

    if (start2) //if re button click moves to targer-2
    {
        agent.SetDestination(target2.position);
        start2=false;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53042175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档