Unity3d 如何将不跟随的对象统一起来

Unity3d 如何将不跟随的对象统一起来,unity3d,Unity3d,当我开枪时,它会追赶敌人。我想要一个瞄准系统, 例如,子弹应该在第一次看到敌人时射向敌人的位置,而不是追逐目标,只是射向敌人看到的第一个位置。 这是我的密码,它使子弹跟随敌人: void shoot() { GameObject bulletGO=(GameObject) Instantiate(BulletPrefab, firepoint.position, firepoint.rotation); Bullet bullet = bulletGO.GetCompon

当我开枪时,它会追赶敌人。我想要一个瞄准系统, 例如,子弹应该在第一次看到敌人时射向敌人的位置,而不是追逐目标,只是射向敌人看到的第一个位置。 这是我的密码,它使子弹跟随敌人:

 void shoot() {
     GameObject bulletGO=(GameObject)  Instantiate(BulletPrefab, firepoint.position, firepoint.rotation);
     Bullet bullet = bulletGO.GetComponent<Bullet>();
     if (bullet != null) {
         bullet.Seek(target);
     }
以及:

有什么想法吗?

替换

private Transform target;
//... 
public void Seek(Transform _target) {
target = _target;
}
//...
Vector3 dir = target.position - transform.position;

通过这种方式,您可以计算并复制Seek中现有的目标位置,并使子弹飞到该位置,即使实际目标移动了。每次调用更新时,您实际占据当前目标位置的代码

private Transform target;
//... 
public void Seek(Transform _target) {
target = _target;
}
//...
Vector3 dir = target.position - transform.position;
private Vector3 target;
//...
public void Seek(Transform _target) {
target = _target.position;
}
//...
Vector3 dir = target - transform.position;