Question by
Yzii · Feb 03, 2016 at 07:43 PM ·
c#gameobjectraycastlayermaskphysics.raycast
How actually work LayerMasks in Physics.Raycast ()?
Hello,
I want to find out if specific object was hit so I have GameObject and i want to read its layerMask. All is working to this point, but when I try to get this readed value into Physics.Raycast, its not working. I`ve tried it with public LayerMask so i had to choose it in Unity and it worked fine (but thats not what i want).
So is there any way to get Physics.Raycast work with LayerMask readed from GameObject?
using UnityEngine;
using System.Collections;
public class SpeechBubbles : MonoBehaviour {
private Camera cam;
private LayerMask sourceMask1;
private GameObject source1;
public LayerMask testLayer;
// Use this for initialization
void Start () {
cam = Camera.main;
source1 = GameObject.Find ("Trader");
Debug.Log (source1.name);
sourceMask1 = source1.layer;
Debug.Log (sourceMask1.value);
}
// Update is called once per frame
void Update () {
request ();
}
void request (){
if (Input.GetMouseButtonDown (0)) {
hitted ();
}
}
public void hitted (){
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, Mathf.Infinity, sourceMask1)) {
Debug.Log ("Trader here!");
} else{
Debug.Log("nothing here");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer
how to get name of game object with position by raycast 1 Answer
Need help with Ray casting syntax. 1 Answer
No gameobject instance again in update 1 Answer
Unable to correctly use game objects? 2 Answers