Unity Physics.Raycast 不应该检测到对象 [英] Unity Physics.Raycast does not detect an object when it should

查看:61
本文介绍了Unity Physics.Raycast 不应该检测到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 2D 游戏,我正在使用 Raycast 来检测玩家是否在地面上.无论什么 Raycast 总是返回 false.我使用了 Debug.DrawRay,它显示光线正在撞击地面.我尝试了几个不同教程的解决方案,但没有任何效果.我对 Unity 还很陌生,所以希望更有经验的人知道出了什么问题.

I am making a 2D game and I am using Raycast to detect if the player is on the ground. No matter what Raycast always returns false. I have used Debug.DrawRay which shows that the ray is hitting the ground. I have tried solutions from several different tutorials but nothing worked. I am pretty new to Unity so hopefully someone who is more experienced knows what's wrong.

 if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.down), DistanceDown, LayerMask)){
        Debug.Log("Grounded");
        return true;
    }
    else
    {
        Debug.Log("Not Grounded");
        return false;
    }

修订代码:

if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), (float)(GetComponent<Collider2D>().bounds.extents.y + Offset)))

推荐答案

Raycast 有几个参数.我将解释每个原因,因为可能有几个原因导致它无法正常工作,因此可能将其分解可以帮助您了解问题所在.

A Raycast has a few parameters. I will explain each as there could be a few reason as to why it is not working, so possibly breaking this up can help you realize what's wrong.

Raycast(Vector3 origin, Vector3 direction, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

原点是光线开始的空间点.目前,您将其设为 transform.position,因此它将从脚本所在的位置开始,很可能是您的播放器.

The origin is the point in space that the ray will start from. Currently, you have it as transform.position, so it will start at the position of whatever the script is on, most likely your player.

方向 是一个应该归一化的向量,因此没有比例尺来确定光线应该从原点投射到哪里.现在你正在使用 -Vector3.down,这将导致 Vector3.Up 因为向上和向下是相反的,所以否定一个给另一个.对于地面检查,我将使用 Vector3.down,因为地面通常在玩家的原点下方或下方.

The direction is a vector that should be normalized so there is no scale to determine where from the origin the ray should be cast. Right now you are using -Vector3.down, which would result in Vector3.Up as up and down are opposites, so negating one gives the other. For a ground check, I would be using Vector3.down as the ground is generally down or below the origin of your player.

ma​​xDistance 是光线投射的长度.现在你把它作为 DistanceDown 我假设它是一些浮动.通常,对于地面检查,我会使用您角色的碰撞器,然后添加一些偏移量以确定何时接地.类似于 GetComponent().bounds.extents.y + OFFSET 的内容.bounds.extents.y 是对撞机大小的一半,并且由于您的原点是玩家的中心,因此它应该落在对撞机的脚或底部,这就是您需要偏移量的原因.

The maxDistance is the length that the ray is cast. Right now you have it as DistanceDown which I am assuming is some float. Generally, for a ground check, I would take the collider of your character, then add some offset to determine when grounded. Something along the lines of GetComponent<Collider>().bounds.extents.y + OFFSET. The bounds.extents.y is half the size of the collider and since your origin is the center of your player, this should land at the feet or base of your collider, which is why you need the offset.

layermask 用于在投射时忽略特定层.当您只想投射并击中单个图层时,它非常有用,在您的情况下可能只是地面.我没有看到这方面的代码,但我会提供一个例子,以防万一这是你出错的地方.LayerMask mask = LayerMask.GetMask("GroundLayer"); 并使用 mask 作为你的面具.现在光线只会击中 GroundLayer 层上的对象.

The layermask is used to ignore particular layers when casting. It is rather useful when you only want to cast and hit a single layer, where in your case could just be the ground. I do not see code for this, but I will provide an example in case this is where you are going wrong. LayerMask mask = LayerMask.GetMask("GroundLayer"); and use mask as your mask. Now the ray will only hit objects on the layer GroundLayer.

您未使用的最后一个参数称为 queryTriggerInteraction.Raycast 默认命中触发器,但使用此参数,您可以覆盖它并告诉它命中触发器.

The last parameter which you are not using is called queryTriggerInteraction. Raycast by default do not hit triggers, but with this parameter, you can override it and tell it to hit triggers.

通读每个部分并检查以确保您所做的与我所描述的相符.如果您仍然无法根据我所写的内容找出您的问题,我需要更多地了解您的情况以提供帮助.

Read over each of these sections and check to make sure what you are doing matches what I am describing. If you are still unable to figure out your issues based on what I have written, I would need to know a bit more about your situation to help out.

这篇关于Unity Physics.Raycast 不应该检测到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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