即使光线沿矢量方向起作用,光线投射目标也不会与父对象一起移动

问题描述

Object facing left at the center of the scene,with the raycast cast with ray of transform.position,transform.forward

Object facing left at the edge of the scene with the same ray,ray still casts to the same point even though the point is being called as a direction

这怎么可能发生?我已经重写了Raycast代码几次,但这是当前的迭代:

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

public class RaycastTest : MonoBehaviour {

    //width will control the distance of the two rays' direction from each other
    //range will control the collision range
    public float width,range;
    //will take width and control bias in the furture
    float b;

    void Start()
    {

    }

    void Update () {
        //loop from negative one to positive one to add left and right rays in the future
        for (int i = -1; i < 3; i += 2)
        {
            //commented line segment showing how bias will work to control the angle of the ray
            //! cant do any of that yet because this ray doesn't work
            Ray ray = new Ray(transform.position,/*(transform.forward + transform.right/2 * i) / 3*/ transform.forward);
            RaycastHit hitInfo;

            if (Physics.Raycast(ray.origin,ray.direction,out hitInfo,range))
            {
                Debug.DrawLine(ray.origin,hitInfo.point,Color.red);
            }
            else
            {
                Debug.DrawLine(ray.origin,ray.direction * range,Color.green);
            }
        }
    }
}

问题可能在第24行或第27行,但是我仍然不知道它是如何出现的。

编辑:我通过将射线方向设置为常数来进行测试:(-1,0,0)(全局左),问题仍然存在,所以它可能是第24行的Ray声明的问题。

解决方法

我忘了在else中将转换添加到debug.drawline

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...