效果演示

发射子弹.gif

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Shoot : MonoBehaviour
{
    // 子弹预制件
    public GameObject bullet;

    public List<GameObject> bullets;

    // 子弹的最远距离
    public float bulletDistance = 10;

    // 发射正方向
    Vector3 RightAheadShootDir;

    // 正前方左偏移15°
    Vector3 RightAheadShootDirLeftOffset15;

    // 正前方右偏移15°
    Vector3 RightAheadShootDirRightOffset15;

    // 子弹移动速度
    float bulletMoveSpeed = 10;

    // 子弹发射模式(单发、双发、群发)
    public BulletMode currentBulletMode = BulletMode.SingleShot;

    private void Start()
    {
        bullets = new List<GameObject>();
        RightAheadShootDir = transform.forward.normalized;
        RightAheadShootDirLeftOffset15 = (Quaternion.AngleAxis(15, transform.up) * transform.forward).normalized;
        RightAheadShootDirRightOffset15 = (Quaternion.AngleAxis(-15, transform.up) * transform.forward).normalized;
    }
    float y;
    // 方块旋转速度
    public float rotationSpeed = 5f;
    private void Update()
    {
        // 方块的旋转
        cubeMove();
        // 输入
        InputAction();
        // 子弹移动
        BulletMove();
        // 子弹销毁
        DestroyBullet();
    }

    void cubeMove()
    {
        y += Input.GetAxis("Mouse X") * rotationSpeed;
        y = Mathf.Clamp(y, -45, 45);
        Quaternion cubeDir = Quaternion.Euler(transform.rotation.eulerAngles.x, y, transform.rotation.eulerAngles.z);
        transform.rotation = Quaternion.Lerp(transform.rotation, cubeDir, 0.9f);
    }

    void InputAction()
    {
        if (Input.GetMouseButtonDown(0))
        {
            updateDir();
            if (currentBulletMode == BulletMode.SingleShot)
            {
                // 朝着正前方发射
                GengerateBullet(RightAheadShootDir);
            }
            else if (currentBulletMode == BulletMode.TripleShot)
            {
                GengerateBullet(RightAheadShootDir);
                GengerateBullet(RightAheadShootDirLeftOffset15);
                GengerateBullet(RightAheadShootDirRightOffset15);
            }
            else if (currentBulletMode == BulletMode.MultipleShot)
            {
                for (int i = 1; i <= 24; i++)
                {
                    GengerateBullet(Quaternion.AngleAxis(15 * i, transform.up) * RightAheadShootDir);
                }
            }
        }
    }

    /// <summary>
    /// 发射时,更新方向
    /// </summary>
    void updateDir()
    {
        RightAheadShootDir = transform.forward.normalized;
        RightAheadShootDirLeftOffset15 = (Quaternion.AngleAxis(15, transform.up) * transform.forward).normalized;
        RightAheadShootDirRightOffset15 = (Quaternion.AngleAxis(-15, transform.up) * transform.forward).normalized;
    }


    /// <summary>
    /// 生成子弹
    /// </summary>
    /// <param name="orientation">设置子弹朝向</param>
    private void GengerateBullet(Vector3 orientation)
    {
        Quaternion dir = Quaternion.LookRotation(orientation);
        var go = GameObject.Instantiate(bullet, transform.position, dir, transform);
        if (go == null) return;
        if(bullets!= null)
        {
            bullets.Add(go);
        }
    }



    /// <summary>
    /// 子弹的移动
    /// </summary>
    private void BulletMove()
    {
        foreach (var item in bullets)
        {
            var bulletTF = item.transform;
            bulletTF.position += (bulletTF.forward * bulletMoveSpeed * Time.deltaTime);
         }
    }

    /// <summary>
    /// 销毁子弹
    /// </summary>
    private void DestroyBullet()
    {
        for(int i = 0; i<bullets.Count; i++)
        {
            var item = bullets[i];
            if (Vector3.Distance(transform.position, item.transform.position) > bulletDistance)
            {
                Destroy(item);
                bullets.RemoveAt(i);
            }
        }
    }
}

public enum BulletMode
{
    SingleShot,
    TripleShot,
    MultipleShot
}