【问题标题】:Unity Ray-cast not specific enoughUnity Ray-cast 不够具体
【发布时间】:2022-01-23 08:45:58
【问题描述】:

光线投射似乎只在我移动和随机定位时更新,任何帮助将不胜感激。

    `    void Update(){
    if(IsBruiserFollowingMarkerRay){
        RaycastHit hit;
        BruiserMoveToVisuals.SetActive(true);
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, Range)){
       // Debug.Log(hit.transform.position);
        BruiserMoveToDefault.transform.position = hit.transform.position;
        }
    }`

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    一旦光线投射无法按预期工作,请使用 Debug.DrawRay() 直观地查看发生了什么。像这样的:

        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, Range))
        {
            Debug.DrawRay(fpsCam.transform.position, fpsCam.transform.forward * Range, Color.yellow, 1f);
            // ...
        }
        else
        {
            Debug.DrawRay(fpsCam.transform.position, fpsCam.transform.forward * Range, Color.green, 1f);
        }
    

    请注意,我们需要将方向与范围相乘,以直观地显示与 Raycast() 长度相同的射线。您现在应该能够看到为什么光线投射没有给出您期望的答案。如果您看不到任何光线(无论是黄色还是绿色),可能是 Range 为 0 或 fpsCam 未定位到您期望的位置。

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2015-10-06
      相关资源
      最近更新 更多