- Home /
Raycasting crashing Unity?
I've got a script that (while a player hasn't been sighted) sends out various raycasts. If one of these raycasts hits a player, the loop ends and the AI should begin to give chase. The chase portion of the script does nothing right now, but something is causing Unity to crash when I click "play".
Edit: Despite using the code format button, my code formatting gets destroyed. Any help with making that more readable would be appreciated, too. ;]
void Search(){ RaycastHit hit;
Vector3 rayPos = transform.Find ("rayPos").position;
Vector3 right15 = Quaternion.Euler(0, 15, 0) transform.forward; Vector3 right30 = Quaternion.Euler(0, 30, 0) transform.forward; Vector3 right45 = Quaternion.Euler(0, 45, 0) * transform.forward;
Vector3 left15 = Quaternion.Euler (0, -15, 0) transform.forward; Vector3 left30 = Quaternion.Euler (0, -30, 0) transform.forward; Vector3 left45 = Quaternion.Euler (0, -45, 0) * transform.forward;
// [LINE OF SIGHT CHECKS] // Forward
while(playerSighted != true){ // Forward if(Physics.Raycast(rayPos, transform.forward, out hit, 30.0f)){ Debug.Log("Hit: 0 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 15 degrees right if(Physics.Raycast(rayPos, right15, out hit, 26.0f)){ Debug.Log ("Hit: 15 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 30 Degrees right if(Physics.Raycast(rayPos, right30, out hit, 22.0f)){ Debug.Log ("Hit: 30 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 45 Degrees right if(Physics.Raycast(rayPos, right45, out hit, 18.0f)){ Debug.Log ("Hit: 45 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 15 degrees left if(Physics.Raycast(rayPos, left15, out hit, 26.0f)){ Debug.Log ("Hit: -15 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 30 degrees left if(Physics.Raycast(rayPos, left30, out hit, 22.0f)){ Debug.Log ("Hit: -30 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } }
// 45 degrees left if(Physics.Raycast(rayPos, left45, out hit, 18.0f)){ Debug.Log ("Hit: -45 Degrees"); Debug.DrawLine(rayPos, hit.point, Color.green); if(hit.collider.tag == "Player"){ playerSighted = true; } } }
if(playerSighted == true){ Chase(); }
}
Apologies for that being lengthy. Any ideas would be greatly appreciated. Thanks!
Answer by m.alekseev · Jul 23, 2012 at 09:00 PM
Check if you have collider on your player object. Or maybe any ray catches other game object, which doesn't have collider.
Answer by Razion · Jul 24, 2012 at 08:23 AM
Turned out to be an issue with unity not liking the way I was making the raycasting conditional. Not quite sure why.
Answer by Sphax84 · Nov 28, 2016 at 12:31 AM
I get a crash on "Physics.Raycast (ray, hit)" as well on x64 (not tested on x86) when many raycasts are achieved in a loop. This happens in editor while playing or in standalone exactly the same.
Related thread: http://answers.unity3d.com/questions/240483/raycast-crashing-build-on-collision-with-mesh-coll.html#answer-1277231
Your answer
Follow this Question
Related Questions
Another Raycast Issue. 0 Answers
Finding RayCastHit's Origin Position 2 Answers
Diagonal Raycasting to detect platform returns true, even when false 1 Answer
AI Raycast problem 1 Answer
Raycasting and enemies ai 2 Answers