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

Unity2D中如何将Vector2方向偏移20度?射线旋转实现求助

Unity 2D实现正方向及两侧偏移射线方案

问题说明

需要发射三条射线:沿物体自身右方向(已跟随鼠标旋转)、向左偏移20度、向右偏移20度。原代码中transform.Rotate_Direction是错误写法,Unity无此方法,以下是正确实现步骤及代码。

核心原理

在Unity 2D中,旋转向量需通过**四元数(Quaternion)**实现:

  • 用Quaternion.AngleAxis(角度, 旋转轴)创建旋转规则(2D场景下旋转轴固定为Vector3.forward,即屏幕垂直方向)
  • 将旋转四元数与原方向向量相乘,得到偏移后的方向向量

完整修改代码

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

public class PathFinding : MonoBehaviour
{
    public float speed = 5f;
    // 射线偏移角度,可随时调整
    public float rayOffsetAngle = 20f;
    // 射线长度
    public float rayLength = 10f;

    private void Update()
    {
        // 计算鼠标指向方向并旋转物体
        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);

        if (Input.GetKey(KeyCode.Space))
        {
            // 1. 获取物体当前的正前方(自身右方向,已考虑旋转)
            Vector2 forwardDir = transform.TransformDirection(Vector2.right);

            // 2. 绘制并检测正前方射线
            Debug.DrawRay(transform.position, forwardDir * rayLength, Color.green);
            RaycastHit2D frontHit = Physics2D.Raycast(transform.position, forwardDir, rayLength);
            if (frontHit)
            {
                Debug.Log("正前方命中: " + frontHit.collider.name);
                Debug.DrawRay(transform.position, forwardDir * rayLength, Color.red);
            }

            // 3. 生成向左偏移20度的方向
            Quaternion leftRotation = Quaternion.AngleAxis(rayOffsetAngle, Vector3.forward);
            Vector2 leftDir = leftRotation * forwardDir;
            // 绘制并检测左偏移射线
            Debug.DrawRay(transform.position, leftDir * rayLength, Color.yellow);
            RaycastHit2D leftHit = Physics2D.Raycast(transform.position, leftDir, rayLength);
            if (leftHit)
            {
                Debug.Log("左偏移命中: " + leftHit.collider.name);
                Debug.DrawRay(transform.position, leftDir * rayLength, Color.red);
            }

            // 4. 生成向右偏移20度的方向(负角度表示顺时针旋转)
            Quaternion rightRotation = Quaternion.AngleAxis(-rayOffsetAngle, Vector3.forward);
            Vector2 rightDir = rightRotation * forwardDir;
            // 绘制并检测右偏移射线
            Debug.DrawRay(transform.position, rightDir * rayLength, Color.cyan);
            RaycastHit2D rightHit = Physics2D.Raycast(transform.position, rightDir, rayLength);
            if (rightHit)
            {
                Debug.Log("右偏移命中: " + rightHit.collider.name);
                Debug.DrawRay(transform.position, rightDir * rayLength, Color.red);
            }
        }
    }
}

关键代码解释

  1. 获取当前正方向:transform.TransformDirection(Vector2.right)

    • 将局部坐标系的右方向(Vector2.right)转换为世界坐标系向量,自动适配物体当前的旋转角度,确保射线始终跟随物体朝向。
  2. 创建旋转四元数:Quaternion.AngleAxis(rayOffsetAngle, Vector3.forward)

    • rayOffsetAngle是偏移角度,正数值表示逆时针旋转(向左偏移),负数值表示顺时针旋转(向右偏移)。
    • Vector3.forward是2D场景的旋转轴,对应屏幕垂直向外的方向。
  3. 生成偏移方向:leftRotation * forwardDir

    • 四元数与向量相乘,本质是将向量按照四元数定义的规则旋转,得到偏移后的世界空间方向。
  4. 调试射线:Debug.DrawRay

    • 不同颜色区分三条射线,命中时切换为红色,方便在Scene窗口直观查看射线状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 09:03:50