ShotGun Spread Unity 3D FPS стрелок - PullRequest
0 голосов
/ 03 апреля 2020

Я пытаюсь сделать fps-игру, и у меня внедрена система инвентаризации. Однако, когда я добавляю дробовик и затем стреляю в него, пули распространяются, но они распространяются по горизонтальной оси, и это выглядит довольно странно. Может ли кто-нибудь рассказать мне, как правильно стрелять из пуль. Вот что у меня так далеко. другая проблема в том, что когда игрок смотрит налево, пули не будут распространяться, но вместо этого они будут 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);
        }

    }

Заранее спасибо.

1 Ответ

0 голосов
/ 04 апреля 2020

Рассмотрите возможность использования Camera.ScreenPointToRay

Я бы также порекомендовал вам распределить пули в координатах мирового пространства, так как пространство экрана будет зависеть от размера экрана.

Пример кода:

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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...