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

Unity炮塔Raycast仅在特定时长内命中敌人问题求助

问题描述
  • 射击时,炮塔仅能在约4秒内记录对敌人的命中,之后完全无法检测到命中。
  • 排查发现:超过该时长后,Raycast完全无法击中Enemy层的敌人,场景中所有已加载的敌人均存在此问题。
炮塔控制器代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurretController : MonoBehaviour
{
    public float rotationSpeed = 5f;
    public float fireRate = 1f;
    public GameObject projectilePrefab;
    public Transform projectileSpawnPoint;
    public Transform RotationPoint;
    public float maxZoom = 12.75f;
    public float minZoom;
    public float ZoomScale;
    public Camera TurretCam;
    public float ZoomLevel;
    public float damage;
    public LayerMask hitMask;

    public bool isPlayerInTurret = false;
    private float timeSinceLastShot = 0f;

    void Update()
    {
        if (!isPlayerInTurret) TurretCam.gameObject.SetActive(false);
        if (isPlayerInTurret)
        {
            TurretCam.gameObject.SetActive(enabled);
            // 获取输入
            float horizontal = Input.GetAxis("Mouse X");
            float vertical = Input.GetAxis("Mouse Y");

            // 炮塔相机缩放级别
            float input = Input.mouseScrollDelta.y * ZoomScale * Time.deltaTime;
            ZoomLevel = Mathf.Clamp(input + ZoomLevel, maxZoom, minZoom);
            TurretCam.fieldOfView = ZoomLevel;



            // 旋转炮塔
            RotationPoint.Rotate(Vector3.up * horizontal * rotationSpeed * Time.deltaTime);
            RotationPoint.Rotate(Vector3.right * -vertical * rotationSpeed * Time.deltaTime);
            Quaternion rotation = RotationPoint.transform.rotation;
            rotation = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y, 0f);
            RotationPoint.transform.rotation = rotation;

            // 射击逻辑
            timeSinceLastShot += Time.deltaTime;
            //if (Input.GetButtonDown("Fire1") && timeSinceLastShot >= 1f / fireRate)
            if (Input.GetButton("Fire1"))
            {
                print("click");
                timeSinceLastShot = 0f;
                RaycastHit hit;
                Debug.DrawLine(projectileSpawnPoint.transform.position, projectileSpawnPoint.transform.forward*1000, Color.red, 20f);
                if (Physics.Raycast(TurretCam.transform.position, TurretCam.transform.forward, out hit, Mathf.Infinity, hitMask))
                {
                    print("Ray has hit");
                    print(hit.collider.TryGetComponent<Enemy>(out Enemy enemy) + hit.transform.gameObject.name);
                    if (enemy != null)
                    {
                        enemy.takeDamage(damage);
                    }
                }
            }
        }

        // 进入/退出炮塔
        if (Input.GetKeyDown(KeyCode.E))
        {
            /// TODO
        }
    }
}
敌人脚本代码
using System.Collections;
using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
    public GameObject Objective;
    private NavMeshAgent agent;
    public float health = 100f;
    public GameObject explosion;
    public Vector3 ExplosionAdd;

    // 初始化
    void Start()
    {
        agent = gameObject.GetComponent<NavMeshAgent>();
        gameObject.GetComponent<NavMeshAgent>().enabled = true;
        transform.parent = null;
    }
    bool hasExploded = false;
    // 每一帧更新
    void Update()
    {
        if (hasExploded) return;
        //agent.destination = Objective.transform.position;
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "Explode!") Explode();
    }

    public void takeDamage(float damage)
    {
        print("x");
        health -= damage;
    }
    public void Explode()
    {
        hasExploded = true;
        Objective.GetComponent<Objective>().TakeDamage(100);
        GameObject x = Instantiate(explosion, null);
        x.transform.position = gameObject.transform.position + ExplosionAdd;
        Destroy(x, 2);

        Destroy(gameObject);
    }
}

内容的提问来源于stack exchange,提问作者Luca Lowndes

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 22:25:04