Quantcast
Channel: Questions in topic: "drawray"
Viewing all articles
Browse latest Browse all 92

LayerMask, DrawRay and DrawLine acting weird...

$
0
0
Ok, so I am getting some odd results when I use Physics.DrawRay(). I have created a function for detecting collisions with other game objects on Layer 10. Unfortunately, while the ray passes through the object, it doesn't record a collision. (Yes I am certain the object has a Mesh Collider on the appropriate layer). However, if I then use my LayerMask, none of the collisions happen, and it tells me the length must be greater than 0. (I am trying to ignore Layer 0). In the image below, Red rays indicate no hit, Yellow means the collider is not hitting the appropriate object, and green means it is correct. Here is a code snippet: GameObject MakeRayCasters(Vector3[] corners){ GameObject origin = new GameObject("RayCaster"); Vector3 average = new Vector3(); for (int i = 0; i < corners.Length; i++) { average += corners[i]; } average /= corners.Length; RaycastHit hit; Vector3 PlanarUp = Vector3.Cross((Vector3.zero - northPoints[1]).normalized,(Vector3.zero - northPoints[0]).normalized); LayerMask ignoreLayer = new LayerMask(); ignoreLayer.value = 0; origin.transform.position = average; origin.transform.localRotation = Quaternion.LookRotation((Vector3.zero - origin.transform.position).normalized,PlanarUp); if (Physics.Raycast(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50, out hit)) { if (hit.collider.gameObject.layer==10) { Debug.Log(hit.collider.gameObject.name); Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.green,Mathf.Infinity); } else { Debug.Log("INVALID HIT: " + hit.collider.gameObject.name); Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.yellow,Mathf.Infinity); } } else { Debug.DrawRay(origin.transform.position+(origin.transform.up*50),-origin.transform.up*50,Color.red,Mathf.Infinity); Debug.Log("MISS: " + hit.point); } RayCasters.Add(origin); return origin; } Here is a screenshot: ![alt text][1] [1]: /storage/temp/10504-drawrays.jpg

Viewing all articles
Browse latest Browse all 92

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>