我已经创建了navMeshand代理。对于目标,我使用了两个空对象。对于每个空对象,我创建了两个按钮。
如果我单击白色按钮,代理首先移动到空目标,然后再单击红色按钮代理移动到第二个空目标。
当我想要将代理从target-2移动到target-1时,我将面临问题。我怎么能把那个探员转移到taeget-1?
视频链路https://youtu.be/zRKHdMeQsi0
代码
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);
}
}
}发布于 2018-10-29 09:24:52
也许这会有帮助。
public static void buttonClick()
{
//if white button click
start1 = true;
start2 = false;
}
public static void buttonClick2()
{
//if red button click
start2 = true;
start1 = false;
}发布于 2018-10-29 09:59:50
您忘记了通过将布尔值重置为false来替代状态。因为您已经在按钮中设置了布尔值,所以您也可以在update函数中替换状态。
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;
}
}https://stackoverflow.com/questions/53042175
复制相似问题