Unity3d 如何修复追逐/攻击代码?

Unity3d 如何修复追逐/攻击代码?,unity3d,transform,Unity3d,Transform,因此,当前代码的工作方式是,敌人将找到玩家并向他移动。当他找到他时,他会停下来然后开始攻击。如果玩家离开,敌人将停止攻击,只是坐在那里,直到玩家回到射程。我如何修复它,当玩家移动到射程之外时,敌人会再次开始追逐,然后像往常一样攻击 float moveSpeed = 3f; float rotationSpeed = 3f; float attackThreshold = 3f; //distance within which to attack float chaseThreshold =

因此,当前代码的工作方式是,敌人将找到玩家并向他移动。当他找到他时,他会停下来然后开始攻击。如果玩家离开,敌人将停止攻击,只是坐在那里,直到玩家回到射程。我如何修复它,当玩家移动到射程之外时,敌人会再次开始追逐,然后像往常一样攻击

float moveSpeed = 3f;
float rotationSpeed = 3f;
float attackThreshold = 3f;   //distance within which to attack
float chaseThreshold = 10f;   //distance within which to start chasing
float giveUpThreshold = 20f;  //distance beyond which AI gives up
float attackRepeatTime = 1f;  //time between attacks
bool attacking = false;
bool chasing = false;
float attackTime;
Transform target;             //the enemy's target
Transform myTransform;        //current transform data of the enemy

void Update()
{
     //rotate to look at the player
     float distance = (target.position - myTransform.position).magnitude;
     if (chasing)
     {
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
     }

     //move towards the player
     if (chasing == true && attacking == false)
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

     //give up if too far away
     if (distance >= giveUpThreshold)
     {
         chasing = false;
     //    attacking = false;
     }

     //attack, if close enough, and if time is OK
     if (distance <= attackThreshold && Time.time >= attackTime) //if attacking we want to stop moving
     {
         //attack here
         bossAttack.Attack();
         attackTime = Time.time + attackRepeatTime;
         print("Attacking!");
         attacking = true;
       //  anim.SetTrigger("AutoAttack");
         chasing = false;
     }
     else
     {
         //not currently chasing.
         //start chasing if target comes close enough
         if (distance <= chaseThreshold)  //if he gets to chase, and then you move out of range again, he won't chase again. he will only attack if comes into range again
         {
             chasing = true;
           //  attacking = false;
             //   print("Chasing!");
         }
     }
 }
float-moveSpeed=3f;
浮动旋转速度=3f;
浮动攻击阈值=3f//攻击距离
浮动阈值=10f//开始追逐的距离
浮动给定阈值=20f//AI放弃的距离
浮动攻击重复时间=1f//攻击间隔时间
布尔攻击=假;
bool=false;
浮动攻击时间;
转化目标//敌人的目标
转换myTransform//敌方当前转换数据
无效更新()
{
//旋转看球员
浮动距离=(target.position-myTransform.position).magnity;
如果(追逐)
{
myTransform.rotation=Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position-myTransform.position),rotationSpeed*Time.deltaTime);
}
//向玩家移动
如果(追逐==真&&攻击==假)
myTransform.position+=myTransform.forward*moveSpeed*Time.deltaTime;
//太远就放弃
如果(距离>=给定阈值)
{
追逐=假;
//攻击=错误;
}
//攻击,如果足够近,如果时间合适
if(distance=attackTime)//如果攻击我们要停止移动
{
//攻击这里
bossAttack.Attack();
attackTime=Time.Time+attackRepeatTime;
打印(“攻击!”);
攻击=正确;
//动画设置触发(“自动攻击”);
追逐=假;
}
其他的
{
//目前没有追逐。
//如果目标足够接近,开始追逐

if(distanceif(chasing==true&&attacking==false)语句
if(chasing==true&&attacking==false)
意味着追逐必须为true,攻击必须为false,但攻击在第一次攻击后不会设置为false(您已经注释掉了所有
attacking=false
行).

有些我无法启用,除非添加其他内容,因为即使在阈值之后,敌人也会继续追击。我甚至添加了if(distance>=chaseThreshold&&distance=giveUpThreshold){chasing=false;attacking=false;},所以我仍然在某个地方缺少它,但我找不到它