简体   繁体   English

如何保持光线投射相对于它绑定的 object?

[英]How do I keep raycast relative to the object its bound to?

I have a car game and I would like to use raycast to detect other obstacles and do stuff if the ray hits.我有一个汽车游戏,我想使用光线投射来检测其他障碍物并在光线命中时做一些事情。 The problem is after the car does a 90 degree turn the raycast move in a way I did not intend them to, like this:问题是在汽车进行 90 度转弯后,光线投射以一种我不希望它们的方式移动,如下所示: 这是我第一次绘制光线时的样子

But when I rotate the car by 90 degrees it looks like this:但是当我将汽车旋转 90 度时,它看起来像这样:

旋转 90

I'd like the rays to remain as they are in the first image regardless of the rotation of the car.无论汽车旋转如何,我都希望光线保持在第一张图像中。

Here is the code to cast the rays and to draw them:这是投射光线并绘制它们的代码:

 private void OnDrawGizmos()
{
    sensorStartPosition1 = transform.position + new Vector3(1f, 0.6f, 0f);
    sensorStartPosition2 = transform.position + new Vector3(-1f, 0.6f, 0f);
    Gizmos.DrawRay(sensorStartPosition1, transform.TransformDirection(Vector3.right) * 3.5f);
    Gizmos.DrawRay(sensorStartPosition2, transform.TransformDirection(Vector3.left) * 3.5f);
}

 private void RunSensors()
{
    RaycastHit hit1;
    sensorStartPosition1 = transform.position + new Vector3(1f, 0.6f, 0);
    //dTS = detect traffic signs
    Ray dTS = new Ray(sensorStartPosition1, transform.TransformDirection(Vector3.right));

    if(Physics.Raycast(dTS, out hit1, sensorLen ))
    {

    }
    Debug.DrawRay(sensorStartPosition1, transform.TransformDirection(Vector3.right) * 3.5f);

    RaycastHit hit2;
    sensorStartPosition2 = transform.position + new Vector3(-1f, 0.6f, 0);
    // dLIT1 = detect left incoming traffic
    Ray dLIT1 = new Ray(sensorStartPosition2, transform.TransformDirection(Vector3.left));
    if(Physics.Raycast(dLIT1, out hit2, sensorLen))
    {

    }
    Debug.DrawRay(sensorStartPosition1, transform.TransformDirection(Vector3.left) * 3.5f);
}

what am I doing wrong?.. thanks in advance我做错了什么?..提前谢谢

You calculate the two start positions with an offset along the global world space X axis regardless of the rotation in无论在

sensorStartPosition1 = transform.position + new Vector3(1f, 0.6f, 0f);

//...

sensorStartPosition2 = transform.position + new Vector3(-1f, 0.6f, 0f);

instead rather calculate them like而是像这样计算它们

sensorStartPosition1 = transform.position + transform.right * 1f + transform.up * 0.6f;

//...

sensorStartPosition2 = transform.position + transform.right * -1f + transform.up * 0.6f;

Or simply或者干脆

sensorStartPosition1 = transform.position + transform.rotation * new Vector3(1f, 0.6f, 0f);

//...

sensorStartPosition2 = transform.position + transform.rotation * new Vector3(-1f, 0.6f, 0f);

Btw instead of顺便说一句,而不是

transform.TransformDirection(Vector3.right)
transform.TransformDirection(Vector3.left)

you can also simply use你也可以简单地使用

transform.right
-transform.right

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让光线投射破坏游戏 object - how do i get the raycast to destroy the game object 如何在正确的方向上使用光线投射缩放 object? - How do I scale an object using raycast in the right direction? 给定 TextBox 绑定其 Content 属性,如何访问 Label 对象? - How do I access a Label object, given the TextBox bound its Content property? Unity2D / C#:如何使光线投射忽略它的第一次碰撞? - Unity2D/C#: How do I make a raycast ignore its first collision? 通过使用raycast的鼠标单击拾取对象时,如何检测碰撞或触发? - How do i detect the collision or trigger when object is picked through mouse click with raycast? 更改实例化对象的父对象会更改其位置,如何将其保持在实例化位置 - Changing the parent of an instantiated object changes its position, how do I keep it at the instantiated position 如何正确处置物体? 为什么代码分析不断改变主意? - How do I correctly dispose of an object? Why does Code Analysis keep changing its mind? 如何让实例化的 object 在实例化后继续关注我的播放器? - How do I make an instantiated object keep following my player after its instantiated? 我怎样才能使 object 平滑地旋转到带有光线投射的目标? - How can i make object to rotate smooth towards a target with raycast? 一旦光线投射不再在 object 上,如何禁用组件? - How can I disable a component once a raycast is no longer on an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM