如何更改 Update() unity c# 中的条件
How to change conditions in Update() unity c#
我已经创建了 navMeshand 代理。对于 target 我使用了两个空对象。
我为每个空对象创建了两个按钮。
如果我再次点击 白色按钮 代理移动到空目标 我点击 红色按钮 代理移动到第二个空目标。
当我想将代理从 target-2 移动到 target-1 时,我遇到了问题。
如何将该代理移动到 taeget-1?
See video for better understanding
视频linkhttps://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);
}
}
}
也许这会有所帮助。
public static void buttonClick()
{
//if white button click
start1 = true;
start2 = false;
}
public static void buttonClick2()
{
//if red button click
start2 = true;
start1 = false;
}
当您单击第二个按钮时,两个条件都变为真,并且在每一帧中您都设置了两个不同的目的地。
public Transform dest, target , target2;
public void buttonClick()
{
dest = target;
}
public void buttonClick2()
{
dest = target2;
}
void Update()
{
agent.SetDestination(dest .position);
}
您忘记通过将布尔值重置为 false 来交替状态。由于您在按钮点击处理程序中设置了布尔值,因此您也可以在更新函数中切换状态。
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;
}
}
我已经创建了 navMeshand 代理。对于 target 我使用了两个空对象。 我为每个空对象创建了两个按钮。
如果我再次点击 白色按钮 代理移动到空目标 我点击 红色按钮 代理移动到第二个空目标。
当我想将代理从 target-2 移动到 target-1 时,我遇到了问题。 如何将该代理移动到 taeget-1?
See video for better understanding
视频linkhttps://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);
}
}
}
也许这会有所帮助。
public static void buttonClick()
{
//if white button click
start1 = true;
start2 = false;
}
public static void buttonClick2()
{
//if red button click
start2 = true;
start1 = false;
}
当您单击第二个按钮时,两个条件都变为真,并且在每一帧中您都设置了两个不同的目的地。
public Transform dest, target , target2;
public void buttonClick()
{
dest = target;
}
public void buttonClick2()
{
dest = target2;
}
void Update()
{
agent.SetDestination(dest .position);
}
您忘记通过将布尔值重置为 false 来交替状态。由于您在按钮点击处理程序中设置了布尔值,因此您也可以在更新函数中切换状态。
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;
}
}