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

Unity顶视角3D游戏中Deflector触发后投射物偏转方向错误求助

顶视角3D游戏投射物偏转方向错误问题修复

我在顶视角3D游戏中,尝试让投射物(Fireball)进入名为Deflector(护盾/偏转器)的物体Trigger时实现偏转,要求仅在X、Z轴方向进行偏转,不能影响Y轴,但目前存在偏转方向错误的问题。

Deflector是点击后生成在玩家前方的物体,其组件配置:
Deflector Components

当前偏转效果错误表现:
Problem occuring during deflection

当前使用的Deflector代码:

using UnityEngine;

public class Deflector : MonoBehaviour
{
    public Transform PlayerRef;
    private Rigidbody spellRb;
    private float deflectForceMultiplier = 3f;

    void Start()
    {
        Destroy(gameObject, 1f);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Projectile") || other.CompareTag("Wind")) {
            spellRb = other.GetComponent<Rigidbody>();
            var spell = spellRb.GetComponent<Spell>();
            spell.Deflected = true;

            var initialSpeed = spellRb.linearVelocity.magnitude;
            var reflectDirection = other.transform.position - transform.position;
            reflectDirection.y = 0;
            spellRb.AddForce(reflectDirection.normalized * initialSpeed * deflectForceMultiplier, ForceMode.Impulse);
            Destroy(gameObject);
        }
    }
}

问题根源

当前代码用other.transform.position - transform.position计算的是Deflector指向投射物的方向,这不是正确的反射逻辑——反射方向应该基于投射物的入射路径和Deflector的表面法线来计算,而非两者的位置差。

修复方案

  1. 提取入射方向:获取投射物的运动方向,剔除Y轴分量确保仅在水平面上计算
  2. 确定偏转器法线:取Deflector自身的前方向(适配顶视角的水平偏转需求),同样剔除Y轴
  3. 计算标准反射方向:使用Unity内置的Vector3.Reflect方法生成正确的反射路径,再约束到X/Z平面

修复后的完整代码

using UnityEngine;

public class Deflector : MonoBehaviour
{
    public Transform PlayerRef;
    private Rigidbody spellRb;
    private float deflectForceMultiplier = 3f;

    void Start()
    {
        Destroy(gameObject, 1f);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Projectile") || other.CompareTag("Wind")) 
        {
            spellRb = other.GetComponent<Rigidbody>();
            var spell = spellRb.GetComponent<Spell>();
            spell.Deflected = true;

            // 1. 获取水平入射方向(移除Y轴影响)
            Vector3 incomingDir = spellRb.linearVelocity;
            incomingDir.y = 0;
            incomingDir.Normalize();

            // 2. 获取偏转器的水平法线(使用自身前方向)
            Vector3 deflectorNormal = transform.forward;
            deflectorNormal.y = 0;
            deflectorNormal.Normalize();

            // 3. 计算水平反射方向
            Vector3 reflectDir = Vector3.Reflect(incomingDir, deflectorNormal);
            reflectDir.y = 0;
            reflectDir.Normalize();

            // 应用偏转力,保留初始速度
            float initialSpeed = spellRb.linearVelocity.magnitude;
            spellRb.AddForce(reflectDir * initialSpeed * deflectForceMultiplier, ForceMode.Impulse);

            Destroy(gameObject);
        }
    }
}

额外注意事项

  • 若偏转方向仍不符合预期,检查Deflector生成时的旋转是否正确,确保transform.forward是你需要的偏转法线方向(可替换为transform.right尝试)
  • 如果投射物的Spell类中有持续更新速度的逻辑,建议在Deflected = true后禁用该逻辑,避免与偏转力产生冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 13:53:10