如何在玩家保持静止而其他一切都在移动的情况下改善敌人的瞄准?

How to improve targeting of enemy where the player stays stationary while everything else moves?

我正在制作一款无限跑酷游戏,使用无限跑酷引擎,其工作原理是让 player/runner 在背景元素、平台和敌人接近玩家时保持在同一位置。

在我引入敌人射击(敌人定位并射击玩家)之前,这一切都很好。我正在使用 EnemyBullet 预制件上的以下脚本:

void Start()
    {


        shooter = GameObject.FindObjectOfType<Player>();
        shooterPosition = shooter.transform.position;
        direction = (shooterPosition - transform.position).normalized;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position += direction * speed * Time.deltaTime;
    }

这可行,但问题是它太准确了,就像即使玩家在跳跃时,子弹预制件也往往会猜测其位置并直接射向玩家。

我知道这是因为玩家实际上并没有移动,这使得玩家的瞄准变得非常容易。

有什么办法可以改善吗?或者以某种方式使其更自然?

谢谢

问题出在代码中

shooter = GameObject.FindObjectOfType<Player>();
shooterPosition = shooter.transform.position;
direction = (shooterPosition - transform.position).normalized;

由于 shooter.transform.position 是玩家的位置,transform.position 是子弹的位置,因此 shooterPosition - transform.position 为您提供了一个直接从子弹位置指向玩家位置的向量。

听起来你想做的是给项目符号添加一些不准确的地方(如果我错了请纠正我)。你可以用这样的东西来做(我假设这是二维的?)

shooter = GameObject.FindObjectOfType<Player>();
shooterPosition = shooter.transform.position;
direction = (shooterPosition - transform.position).normalized;

// Choose a random angle between 0 and maxJitter and rotate the vector that points 
// from the bullet to the player around the z-axis by that amount.  That will add
// a random amount of inaccuracy to the bullet, within limits given by maxJitter
var rotation = Quaternion.Euler(0, 0, Random.Range(-maxJitter, maxJitter));
direction = (rotation * direction).normalized;