You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 3D项目中如何实现武器开火时的子弹曳光效果?

复古FPS游戏子弹曳光系统实现求助

我正在开发一款简单的复古风格第一人称射击(FPS)Unity游戏,目前卡在了武器开火的子弹曳光系统实现上——开火时完全没有视觉反馈,哪怕是最基础的实现也可以,这样能方便我测试武器相关功能。

我已经搭好了枪支的开枪、换弹脚本和系统,也看过相关教程及帖子,但还是没法解决问题。试过用代码创建轨迹并实例化,也试过用预制体,都没成功。

以下是我当前使用的代码,求问如何实现子弹曳光效果:

Gun.cs - 主武器脚本

using System.Collections;
using Assets.Scripts.Player;
using Assets.Scripts.Weapons2;
using UnityEngine;
using UnityEngine.UI;

public class Gun : MonoBehaviour
{
    [Header ("References")]
    [SerializeField] private GunsData gunsData;
    [SerializeField] private Transform cam;

    [Header("UI")]
    [SerializeField] private Text ammoDisplay;

    // Creating fields for adding in other objects such as our actual weapon script; player camera and text canvas

    float timeSinceLastShot;
    int localAmmo;
    private void Start()
    {
        PlayerShoot.shootInput += Shoot;
        PlayerShoot.reloadInput += StartReload;
        localAmmo = gunsData.magSize;
        AmmoCounter();
    }

    private void OnDisable()
    {
        gunsData.reloading = false;
    }

    public void StartReload()
    {
        if (!gunsData.reloading && this.gameObject.activeSelf)
        {
            StartCoroutine(Reload());
        }
    }

    private IEnumerator Reload()
    {
        gunsData.reloading = true;

        yield return new WaitForSeconds(gunsData.reloadTime);

        localAmmo = gunsData.magSize;

        gunsData.reloading = false;

        Debug.Log("Reloaded Weapon"); // Temporary log; since there is no reloading sound or animations yet

        AmmoCounter();
    }

    private bool CanShoot() => !gunsData.reloading && timeSinceLastShot > 1f / (gunsData.fireRate / 60f);

    public void Shoot()
    {
        if (localAmmo > 0)
        {
            if (CanShoot())
            {
                if (Physics.Raycast(cam.position, cam.forward, out RaycastHit hit, gunsData.maxDistance))
                {
                    IDamageable damageable = hit.transform.GetComponent<IDamageable>();
                    damageable?.TakeDamage(gunsData.damage);

                    // Perhaps something to do with tracers would be put here?
                }
                localAmmo--;
                timeSinceLastShot = 0;
                OnGunShot();
                AmmoCounter();
            }
        }
    }

    private void Update()
    {
        timeSinceLastShot += Time.deltaTime;
        Debug.DrawRay(cam.position, cam.forward);
    }

    private void OnGunShot()
    {} // Nothing added here yet

    private void AmmoCounter()
    {
        ammoDisplay.text = $"{localAmmo}/{gunsData.magSize}"; // Related to a text field I have set up
    }
}

PlayerShoot.cs - 射击输入脚本

using System.Collections;
using UnityEngine;

namespace Assets.Scripts.Player
{
    public class PlayerShoot : MonoBehaviour
    {
        public static System.Action shootInput;
        public static System.Action reloadInput;

        [SerializeField] private KeyCode reloadKey;

        private void Update()
        {
            if (Input.GetMouseButton(0))
                shootInput?.Invoke();

            if (Input.GetKeyDown(reloadKey))
                reloadInput?.Invoke();
        }
    }
}

内容的提问来源于stack exchange,提问作者Master Cheeks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:40:18