1
コードが現在どのように動作しているかは、敵がプレイヤーを見つけて彼に向かって移動することです。彼が彼を見つけると、彼は止まり、攻撃を開始する。プレイヤーが遠ざかると、敵は攻撃を止め、プレーヤーが範囲内に戻るまでそこに座ります。プレイヤーが範囲外に移動したときに、敵が再び追いかけて攻撃を開始するように、どのように修正できますか?追跡/攻撃コードを修正するにはどうすればよいですか?
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!");
}
}
}
私はこれがすべて必要な関連コードだと思います。
私は何かを追加せずに有効にすることはできません。なぜなら、敵はスレッシュホールドの後でも追跡を続けるからです。 さらに追加しました if(distance> = chaseThreshold && distance <= giveUpThreshold) { attacking = false; }、次いでコメントアウト 場合(距離> = giveUpThreshold) {追跡= FALSE; attacking = false; } 私はまだどこかでそれを見逃していますが、私はそれを見つけることができません –