- Home /
Raycast 360 degree check?
if (Physics.Raycast (transform.position, Vector3.left, out hit, 2f)
|| Physics.Raycast (transform.position, Vector3.right, out hit, 2f)
|| Physics.Raycast (transform.position, Vector3.forward, out hit, 2f)
|| Physics.Raycast (transform.position, Vector3.back, out hit, 2f)) {
if (hit.transform.tag == "Enemy") {
faces [0].renderer.enabled = false;
faces [3].renderer.enabled = true;
}
} else {
faces [0].renderer.enabled = true;
faces [3].renderer.enabled = false;
}
So, (obviously) I checked the raycast on up, down, left and right, but then I saw that with this kind of coding, raycast doesn't watch diagonally.
So, my question is simple (hopefully the answer is as well), is there a way for the raycast to check a full circle around the player/GameObject instead of just doing what I did above (which, from the size of the If statement, I can already guess is pretty wrong, but hey, gotta start from somewhere, right? :)
Thanks!
Answer by Griffo · Apr 06, 2014 at 06:31 PM
You could try using this
I tried using it, but unsuccessful, sadly, since the SphereCast isn't actually a sphere around the player, but a simple thick raycast. $$anonymous$$aybe I'll use a raycast based on sin and/or cos. $$anonymous$$ake both circle for the duration of 270 - 360 radians. I'll see what happens.
Thanks for the answer in any case! :)
What about Physics.OverlapSphere and use layers to take out the player.
Something like this
#pragma strict
private var whenNeeded : boolean;
function Update(){
if(whenNeeded){
DetectEnemy(transform.position, 20);
}
}
function DetectEnemy(center: Vector3, radius: float) {
var hitColliders = Physics.OverlapSphere(center, radius);
for (var i = 0; i < hitColliders.Length; i++) {
print(hitColliders[i].tag);
// collect information on the hits here
}
}
It works! Thanks you very much! :) I tried putting it in C#, just because I got used to it (not a rocket science, so it was successful), but the idea works very well!
Your answer
Follow this Question
Related Questions
SphereCast Arguments 1 Answer
Debug.DrawLine problem 1 Answer
Having a RaycastHit event create a component? 1 Answer
Problem With RayCast 3 Answers
thousands of raycasts in nested for loop 0 Answers