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

Unity炮塔子弹追踪玩家问题:如何改为朝玩家最后位置直线飞行?

解决炮塔子弹直线飞向玩家最后位置的问题

Got it, let's tweak your code so the bullet flies straight toward the player's position the exact moment it's fired, instead of chasing them nonstop. Here's how to fix it step by step:

1. 调整 TurretBullet 类

Right now your bullet keeps reading the target's real-time position. We need to store a fixed target point instead, so it flies straight to that spot forever:

using UnityEngine;
using System.Collections;

public class TurretBullet : MonoBehaviour {
    // Replace the Transform target with a fixed Vector3 position
    private Vector3 targetPosition;
    public float speed = 70f;

    // Update the method to accept the player's position at fire time
    public void SetTargetPosition(Vector3 _targetPos) {
        targetPosition = _targetPos;
    }

    void Update() {
        float distanceThisFrame = speed * Time.deltaTime;
        Vector3 dir = targetPosition - transform.position;
        
        // Check if we've reached the target point
        if(dir.magnitude <= distanceThisFrame) {
            HitTarget();
            return;
        }

        // Move straight toward the fixed target position
        transform.Translate(dir.normalized * distanceThisFrame, Space.World);
        // Optional: Make the bullet face the direction it's flying
        transform.LookAt(targetPosition);
    }

    void HitTarget() {
        Debug.Log("Player Hit!");
        Destroy(gameObject);
    }
}

2. 修改 Turret 类的 Shoot 方法

When firing the bullet, we need to pass the player's current position (not their Transform reference) so the bullet locks onto that fixed spot:

void Shoot() {
    GameObject bulletGO = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    TurretBullet bullet = bulletGO.GetComponent<TurretBullet>();

    if (bullet != null && target != null) {
        // Pass the player's position at the moment of firing, not their Transform
        bullet.SetTargetPosition(target.position);
    }
}

Why Vector3.forward didn't work for you earlier?

Vector3.forward refers to the bullet's local forward direction. If you tried using it without first pointing the bullet at the target, it would just fly in its initial forward direction instead of toward the player. The code above calculates the correct direction using targetPosition - transform.position, or you could use transform.LookAt(targetPosition) to face the bullet toward the target, then use transform.forward for movement (like replacing the Translate line with transform.Translate(transform.forward * distanceThisFrame, Space.World) — both methods work).

With these changes, once the bullet is fired, it'll fly straight to where the player was when you pulled the trigger, no more tracking their real-time movement.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:35:30