倾斜物理光线投射实现 [英] Slant Physics Raycast Implementation

查看:32
本文介绍了倾斜物理光线投射实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 3d 汽车游戏,我使用物理光线投射来检测障碍物,并根据检测向左或向右转弯.目前,我在做直线光线投射,我无法检测到左右两侧存在的障碍物.

For my 3d car game, I was doing Physics raycast to detect obstacles and based on detect take turn left or right side. Currently, I was doing straight raycast and I can't able to detect left and right side existed obstacles.

所以我决定做一点点倾斜的光线投射来检测左右两侧的东西.下图以更好的方式解释了我的问题:

So I decided to do little bit slant raycast to detect something from the left and right side. The following image explains a better way my question:

此代码目前正在运行:

    Ray rayRight = new Ray(thisTransform.position + Vector3.up * 0.2f + thisTransform.right * detectAngle * 0.5f + transform.right * detectAngle * 0.0f * Mathf.Sin(Time.time * 50), transform.TransformDirection(Vector3.forward) * detectDistance);
    Ray rayLeft = new Ray(thisTransform.position + Vector3.up * 0.2f + thisTransform.right * -detectAngle * 0.5f - transform.right * detectAngle * 0.0f * Mathf.Sin(Time.time * 50), transform.TransformDirection(Vector3.forward) * detectDistance);

    Debug.DrawRay(rayRight.origin, rayRight.direction * detectDistance, Color.red);
    Debug.DrawRay(rayLeft.origin, rayLeft.direction * detectDistance, Color.red);

现在请指导我做倾斜物理射线投射以检测障碍物.

Now please give me a guidance to do slant physics raycast to detect obstacles.

推荐答案

使用与 Vector3.forward 不同的方向?

Use a differnt direction than Vector3.forward?

在你的类中定义两个 Vector3 字段(下面的名称是建议),然后在 Start() 中像这样初始化它们

Define two Vector3 fields in your class (names below are suggestions), then in Start() initalize them like

float angle = 15.0f //or whatever you need.
leftRayDirection = Quaternion.AngleAxis(-angle, Vector3.up) * Vector3.forward;
rightRayDirection = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward;

并使用它们而不仅仅是 Vector3.foward(你仍然需要使用 transform.transformDirection 转换它们).

and use those instead of just Vector3.foward (ofc you still need to transform them, with transform.transformDirection).

或者如果你想让它们扫过,只需在 Update() 中计算它们.

Or if you want them to sweep, just calculate them in Update().

另一种选择是将 2 个四元数存储在一个字段中,并使用它们来旋转 transform.forward(与 transform.transformDirection(Vector3.forward) 相同),例如.

edit: Another option would be to store the 2 Quaternions in a field and use them to rotate transform.forward (which is the same as transform.transformDirection(Vector3.forward)) like.

//Field
Quaternion leftRayRotation;

//in Start()
leftRayRotation = Quaternion.AngleAxis(-angle, Vector3.up);

//before Raycasting
Ray leftRay = new Ray(transform.position + leftRayOffset, leftRayRotation * transform.forward);

这篇关于倾斜物理光线投射实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆