2017-10-17 2 views
0

GVRTeleport 스크립트를 사용하여 카드 보드 앱으로 순간 이동을 허용합니다. 순간 이동을위한 레이 캐스트가 한 레이어를 제외한 모든 것을 무시하기를 바랍니다. this 페이지로 이동합니다. 사과 (사과,이 코드의 소유자에게 원래 링크를 찾을 수 없음)를 수정했지만 현재 텔레포트 스크립트는 아무 것도 볼 수 없습니다. 어떤 아이디어? 내 층 레이어는 레이어 8입니다.이 레이어는 내가이 레이 캐스트와 상호 작용하기를 원합니다.Unity의 특정 레이어에 레이 캐스트

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); 
     } 
    } 
} 

답변

1

먼저 시작하려면 update()의 모든 프레임에 광선을 투영해서는 안됩니다. 엔진이 어떻게 반응할지 모릅니다. 레이를 캐스팅 할 조건을 설정해야합니다 (예 : 키를 누를 때 또는 이와 유사한 경우).

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)) 
{ 

} 

그냥 경우에 당신이 실수로 레이어의 순서를 변경하거나 내 관점에서, 직접 이름을 통과, 향후 수정 : 그럼

대신 마스크에 대한 1 << 8을이 시도 보기, 그것은 더 안전합니다.

문제가 아닌 경우 레이를 캐스팅하기 위해 사용하는 변형이있는 GameObject에서 앞으로 진행되는 항목을 확인하십시오. 어쩌면 당신은 단지 광선이 아무 것도없는 방향으로 던지거나 적어도 그라운드가 아닌 방향으로 던져 넣는 것일 수 있습니다.

+1

그게 효과가 있습니다. 나는 다른 약간의 비트를 바꿨고, 그것을 정리하고 공유하기 위해 게시 할 것이다. – P1505C

+0

완벽한! 좋은 소리 –

관련 문제