2D Bullet于2018.2统一传播

时间:2018-08-11 12:32:47

标签: c# visual-studio unity3d

我实际上是在尝试统一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--;
}

我希望有人能帮助我,对不起,英语不好,我是法语。

1 个答案:

答案 0 :(得分:0)

这个想法不是在弹射器所在的位置发射子弹,而是放置一个偏移值,并在Random.Range(-offset,offset)的帮助下,获得靠近弹射器位置的随机值并使子弹移动到那些位置。

相关问题