简体   繁体   English

ShotGun 传播统一 3d FPS 射手

[英]ShotGun Spread unity 3d FPS shooter

I am trying to make an fps game and I have an inventory system implemented.我正在尝试制作 fps 游戏,并且已经实施了库存系统。 However when I add the shotgun and then shoot it the bullets do spread but they spread on the horizontal axis and it looks kinda odd.但是,当我添加霰弹枪然后射击时,子弹确实会扩散,但它们会在水平轴上扩散,看起来有点奇怪。 Can some one walk me through how to implement proper shooting with bullet spread.有人可以指导我如何通过子弹传播来实现正确的射击。 Here is what I have so far.这是我到目前为止所拥有的。 another issue with that is that when the player is looking left the bullets would not spread but instead they would go in line with each other.另一个问题是,当玩家向左看时,子弹不会扩散,而是会彼此对齐 go。

    public int BulletsShot; // Total bullets show per Shot of the gun
    public float BulletsSpread; // Degrees (0-360) to spread the Bullets
    public GameObject BulletTemplate; // Bullet to fire

    public void Fire()
    {
        float TotalSpread = BulletsSpread / BulletsShot;
        for (int i = 0; i < BulletsShot; i++)
        {
            // Calculate angle of this bullet
            float spreadA = TotalSpread * (i + 1);
            float spreadB = BulletsSpread / 2.0f;
            float spread = spreadB - spreadA + TotalSpread / 2;
            float angle = cam.transform.eulerAngles.y;

            // Create rotation of bullet
            Quaternion rotation = Quaternion.Euler(new Vector3(0, spread + angle, 0));

            // Create bullet
            GameObject bullet = Instantiate(BulletTemplate, tip.position, tip.rotation);
            bullet.GetComponent<Rigidbody>().AddForce(transform.forward * pelletFireVel);
        }

    }

Thanks in advance.提前致谢。

Consider using Camera.ScreenPointToRay考虑使用Camera.ScreenPointToRay

I would also recommend you do the bullet spread in world space coordinates, as screen space will be screen size-dependent.我还建议您在世界空间坐标中进行子弹传播,因为屏幕空间将取决于屏幕尺寸。

Code Example:代码示例:

void SpreadShots(Vector3 screenPos)
{
    int shots = 4;
    float spread = 1f;

    Camera cam = Camera.main;

    for (int i = 0; i < shots; i++)
    {
        Vector3 tapWorldPos = cam.ScreenToWorldPoint(screenPos);

        Vector3 spreadWorldPos = Random.insideUnitCircle * spread;

        Vector3 finalScreenPos = cam.WorldToScreenPoint(tapWorldPos + spreadWorldPos);

        Ray shootRay = cam.ScreenPointToRay(screenPos);

        // Create projectile and shoot it towards shootRay.direction
    }
}

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

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