代码之家  ›  专栏  ›  技术社区  ›  StuckInPhDNoMore

如何在玩家静止不动的情况下提高对敌人的瞄准能力?

  •  0
  • StuckInPhDNoMore  · 技术社区  · 6 年前

    我正在制作一个无限跑者游戏,使用一个无限跑者引擎,它的工作原理是让玩家/跑者保持在相同的位置,同时背景元素、平台和敌人向玩家袭来。

    在我引入敌人射击之前,这一切都很顺利,敌人在这里找到并射击玩家。我使用的脚本如下 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;
        }
    

    这是可行的,但问题是它太精确了,比如即使当玩家跳过子弹时,预制件也倾向于猜测它的位置,并直接向玩家射击。

    我知道这是因为玩家实际上没有移动,这使得锁定玩家非常容易。

    有什么办法可以改进吗?还是在某种程度上让它更自然?

    谢谢

    0 回复  |  直到 6 年前
        1
  •  1
  •   Ben Rubin    6 年前

    问题是在代码中

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

    自从 shooter.transform.position 是玩家的位置和位置 transform.position 那子弹的位置呢 shooterPosition - transform.position 给你一个向量,直接从子弹的位置指向玩家的位置。

    听起来你想做的是给子弹增加一些不准确度(如果我错了,请纠正我)。你可以用这样的东西来做(我假设这是2D?)

    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;