【问题标题】:Raycast to specific layers in Unity [duplicate]光线投射到 Unity 中的特定图层 [重复]
【发布时间】:2017-10-17 20:42:22
【问题描述】:

我正在使用 GVRTeleport 脚本来允许使用纸板应用进行传送。我希望用于传送的光线投射忽略除一层之外的所有内容。通过this 页面,我相应地修改了脚本(抱歉,找不到此代码所有者的原始链接),但现在传送脚本什么也看不到。有任何想法吗?我的地板层是第 8 层,这是我希望此 raycast 与之交互的层。

using UnityEngine;

public class GVRTeleport : MonoBehaviour {

    public float viewHeight = 7f;

    Vector3 fwd;
    public float maxDistance = 10f;
    public LineRenderer line;
    public GameObject parent;
    public GameObject targetIndicator;

    public StraightLineParam genLine;

    int layerMask = 1 << 8;

    void Start() {
    }


    void Update() {
        RaycastHit hit;
        Ray ray;

        if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask)) {
            Debug.Log ("The ray hit the floor");


            if (debugWithMouse) {
                Vector2 mousePos = new Vector2 (Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height);
                ray = Camera.main.ViewportPointToRay (mousePos);
            } else {
                ray = new Ray (transform.position, transform.forward);
            }

            if (Physics.Raycast (ray, out hit)) {
                Debug.DrawLine (transform.position, hit.point, Color.red);  
            }

            if (Input.GetMouseButton (0)) {
                if (Physics.Raycast (ray, out hit)) {

                    if (useViewHeight) {
                        targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
                    } else {
                        targetIndicator.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
                    }

                    targetIndicator.transform.LookAt (hit.point);
                    targetIndicator.GetComponent<Light> ().intensity = 8;

                    genLine.genLine (new Vector3 (ray.origin.x + 2, ray.origin.y - .5f, ray.origin.z), hit.point);

                    line.material.SetTextureOffset ("_MainTex", new Vector2 (Time.timeSinceLevelLoad * -4f, 0f));
                    line.material.SetTextureScale ("_MainTex", new Vector2 (hit.point.magnitude, 1f));
                }
            }

            if (Input.GetMouseButtonUp (0)) {
                if (Physics.Raycast (ray, out hit)) {
                    if (!debugNoJump) {
                        if (useViewHeight) { //better way?
                            parent.transform.position = new Vector3 (hit.point.x, hit.point.y + viewHeight, hit.point.z);
                        } else {
                            parent.transform.position = new Vector3 (hit.point.x, hit.point.y, hit.point.z);
                        }
                    }
                    if (!debugLine) {
                        line.SetVertexCount (0);
                    }
                }
                targetIndicator.GetComponent<Light> ().intensity = 0;
            }
            Debug.DrawRay (this.transform.position, ray.direction * 5, Color.blue);// .DrawLine(transform.position, hit.point, Color.red);
        }
    }
}

【问题讨论】:

    标签: c# unity3d raycasting


    【解决方案1】:

    让我们开始吧,您不应该在update() 的每一帧中都投射光线。不知道引擎会如何反应。你应该首先设置一个投射光线的条件,例如当你按下一个键或类似的时候。

    然后,尝试使用 1 &lt;&lt; 8 代替掩码:

    int layer_mask = LayerMask.GetMask("Floor");
    
    //Actually you can add any layer name you want, for example:
    //int layer_mask = LayerMask.GetMask("Ground","Enemy","Boxes");
    
     //do the raycast specifying the mask
     if (Physics.Raycast (ray, out hit, distance, layer_mask))
     {
    
     } 
    

    以防万一你不小心改变了图层的顺序或者你以后修改它,直接传递名称,从我的角度来看,这样更安全。

    如果这不是问题,请检查您使用其变换来投射光线的游戏对象的前向。也许您只是将光线投射到什么都没有的方向,或者至少没有地面。

    【讨论】:

    • 那行得通。改了几处,整理一下发帖分享。
    • 完美!听起来不错
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多