简体   繁体   English

使用霰弹枪射击 2D 自上而下的射击游戏

[英]Shooting with a shotgun 2D Top-down Shooter

I'm trying to make shooting with a shotgun, I have already made shooting with one bullet.我正在尝试用霰弹枪射击,我已经用一颗子弹射击了。 Code:代码:

            Vector2 shootingDirection = new Vector2(joystick.Horizontal, joystick.Vertical);
            shootingDirection.Normalize();           
            if (shootingDirection != new Vector2(0, 0))
            {
                if(isShotGun) ShotGunShoot(shootingDirection);
                GameObject bullet = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
                bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                bullet.GetComponent<Rigidbody2D>().AddForce(shootingDirection * 10f, ForceMode2D.Impulse);
            }

        }

But I'm just trying to create two other bullets with a slight deviation from the main one, so that it looks like a fraction, but it does not work correctly.但我只是想创建另外两个与主要子弹略有偏差的子弹,这样它看起来像一个分数,但它不能正常工作。 Code:代码:

    private void ShotGunShoot(Vector2 dir)
    {
        GameObject shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
        shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 30f);
        shotGun.GetComponent<Rigidbody2D>().AddForce((dir + new Vector2(-.3f, 0f)) * 10f, ForceMode2D.Impulse);
   
        shotGun = Instantiate(bulletPrefab, crossHair.transform.position, Quaternion.identity);
        shotGun.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg + 30f);
        shotGun.GetComponent<Rigidbody2D>().AddForce((dir+ new Vector2(.3f, 0f)) * 10f, ForceMode2D.Impulse);
    }

As it should be它应该是

Here it is now现在在这里

Thanks for help!感谢帮助!

For the rotation.对于旋转。 You can simply set the direction (from your image I can see that your bullet has to fly towards its right vector) like eg您可以简单地设置方向(从您的图像中我可以看到您的子弹必须飞向其正确的矢量),例如

shotgun.transform.right = dir;
shotgun.transform.Rotate(0, 0, 30);    

For the add force: This uses world space directions.对于附加力:这使用世界空间方向。

You always pass in the direction and add the additional offset in world space.您总是沿方向传递并在世界空间中添加额外的偏移量。 So that additional offset goes always in X axis direction regardless in which direction you shoot.因此,无论您朝哪个方向射击,额外的偏移始终沿 X 轴方向进行。

Rather take the direction into account by using the local force Rigidbody.AddRelativeForce而是通过使用局部力Rigidbody.AddRelativeForce来考虑方向

shotGun.GetComponent<Rigidbody2D>().AddForceRelative((shotgun.transform.right + new Vector2(0f, 0.3f)).normalized * 10f, ForceMode2D.Impulse);

This now uses the bullets right direction and additionally uses an offset of 0.3 along its local Y axis.这现在使用子弹向右的方向,并沿其局部Y 轴另外使用0.3的偏移量。


Btw: If you give your prefab the correct type顺便说一句:如果你给你的预制件正确的类型

public Rigidbody bulletPrefab;

you can skip the GetComponent<Rigidbody> calls.您可以跳过GetComponent<Rigidbody>调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM