【问题标题】:2D Bullet spread on unity 2018.22D Bullet 在统一 2018.2 上传播
【发布时间】:2019-01-18 20:23:36
【问题描述】:

我实际上是在尝试在 Unity 2018 上制作 2D 动作游戏。 我想在用枪射击时进行子弹扩散,我需要指定枪在鼠标光标上射击。我在网上搜索了所有内容,几乎没有找到解决方案。所以我用教程编写的代码不能像我想要的那样工作, 要么子弹散开并朝向光标,但在错误的轴上旋转,并且出现错误。

void Shoot()
{
    Vector3 fireDirection = spawnPoint.right;

    Quaternion fireRotation = Quaternion.LookRotation(fireDirection);

    Quaternion randomRotation = Random.rotation;

    fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, maxBulletSpreadAngle));

    nextFire = Time.time + 1f / fireRate;
    Instantiate(Balle, spawnPoint.position, fireRotation);
    ballesRestantes--;
}

或者离开传播,在好轴上旋转,但不要跟随光标,直接向前射击。

 void Shoot()
{
    Vector3 fireDirection = spawnPoint.forward;

    Quaternion fireRotation = Quaternion.LookRotation(fireDirection);

    Quaternion randomRotation = Random.rotation;

    fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, maxBulletSpreadAngle));

    nextFire = Time.time + 1f / fireRate;
    Instantiate(Balle, spawnPoint.position, fireRotation);
    ballesRestantes--;
}

我希望有人可以帮助我,抱歉英语不好,我是法国人。

【问题讨论】:

  • 他们跟随光标的代码在哪里?
  • 对不起,我解释得不好,子弹不跟随光标,武器射击是我的光标。
  • 我也没有看到代码......看起来你的子弹要么向前射击,要么向右射击,你提供的内容中没有任何内容表明你正在向鼠标射击位置。
  • 类似Vector3 fireDirection = (Camera.ScreenToWorldPoint(Input.mousePosition) - spawnPoint.position).normalized;...
  • 你发布了两段代码,它们是相同的。

标签: c# visual-studio unity3d


【解决方案1】:

这个想法不是在光标所在的位置射击子弹,而是放置一个偏移值,并在 Random.Range(-offset, offset) 的帮助下,您可以获得靠近光标位置的随机值并使子弹飞走到那些位置。

【讨论】:

  • 我不知道该怎么做,但我会尝试的。 (我是一个统一的初学者)
  • 很高兴它有帮助。您能否将我的答案标记为正确答案:) @AlexSoti
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多