【问题标题】:a seemingly simple RaycastALL unity problem I can't figure out一个看似简单的 RaycastALL 统一问题我想不通
【发布时间】:2021-09-20 15:01:30
【问题描述】:

我正在玩游戏,我一直在寻找 3 小时为什么我的 RaycastALL 没有检测到每一次碰撞。这是处理此问题的代码。非常感谢您的宝贵时间。我可以回答您关于我的项目的所有问题:

            case (PlayerState.CarryingRaw):
                //Debug.Log("Carrying" + pickedUpRawItems.Count + "items");
                //Debug.Log(pos.x +","+ pos.y);
                hits = Physics2D.RaycastAll(transform.position, new Vector2(pos.x-5,pos.y),20,8);
                Debug.DrawLine(transform.position, new Vector2(pos.x - 5, pos.y), Color.red, 2);

                for (int i = 0; i < hits.Length; i++)
                    {
                        Debug.Log(hits[i].transform.name);
                    }

                //Debug.Log("Raycasted" + hits.Length + "Objects");
                for (int i = 0; i < hits.Length; i++)
                {
                    {
                        if (hits[i].transform.tag == pickedUpRawItems[pickedUpRawItems.Count - 1].transform.tag)
                        {
                            Debug.Log(hits[i].transform.tag);
                            Debug.Log(pickedUpRawItems[pickedUpRawItems.Count - 1].transform.tag);
                            hits[i].transform.GetComponent<BoxCollider2D>().enabled = false;
                            hits[i].transform.GetComponent<ItemScript>().currentState = ItemScript.ObjectState.PickedUp;
                            hits[i].transform.localScale = sizeChange;
                            pickedUpRawItems.Add(hit.transform.gameObject);
                        }
                    }
                }
                break;
            case (PlayerState.Working):
                break;
            case (PlayerState.Free):
                Debug.Log("youhou + State.Free");
                hit = Physics2D.Raycast(transform.position, new Vector2(pos.x - 5, pos.y));

                if (hit)
                {
                    hit.transform.GetComponent<BoxCollider2D>().enabled = false;
                    Debug.Log("youhou + hit.Free");
                    hit.transform.GetComponent<ItemScript>().currentState = ItemScript.ObjectState.PickedUp;
                    hit.transform.localScale = sizeChange;
                    pickedUpRawItems.Add(hit.transform.gameObject);
                    currentState = PlayerState.CarryingRaw;
                }
                break;

【问题讨论】:

  • doesn't detect every collision .. 例如哪些?

标签: c# unity3d raycasting


【解决方案1】:

没有足够的信息来回答这个问题,但我会开始通过在整个地方添加 Debug.Log() 调用来进行故障排除。注销被击中的对撞机名称,调用它的案例,在所有情况下都使用您的调试行。

出于性能原因,我还建议添加图层蒙版,以免尝试对每个图层进行光线投射。

另外,检查您的物理碰撞设置。如果您转到 Project Settings > Physics2d,您的光线投射对象是否设置为与您尝试进行光线投射的所有对象发生碰撞?

您尝试对其进行射线投射的所有项目是否都有碰撞器?

你的光线投射距离够长吗?

这是一个类似的问题。

https://forum.unity.com/threads/physics2d-raycastall-not-detecting-colliders-minor-problem.862744/

【讨论】:

  • 对您的建议我感激不尽。我的 boxcollider 已启动,但与实际精灵相比非常小。就这么简单。再次感谢您。
  • 如果这解决了您的问题,您应该接受答案(投票箭头下方的小复选标记)。
  • 接受答案可以让其他人知道可能的工作解决方案,同时也增加了回答者的代表。
猜你喜欢
  • 1970-01-01
  • 2011-01-29
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多