- Home /
Check collisions with more than one enemy?
Hi,
How could i do the contrary of this, and only check for collisions between my character and any enemy from the EnemyLayer? (without checking the name of each enemy?)
The layer mask here just "ignores" the specified layer, i would like the exact opposite :
Physics.Linecast(transform.position,player.transform.position,hit,~EnemyLayer.value);
if (hit.collider.name==player.collider.name){
I thought about creating an array of GameObject : `var enemies:GameObject[];` but back to Unity it expects a number with : "Size ---- 0" ...
Any idea?
Thanks
Answer by Adamcbrz · Aug 26, 2012 at 04:35 PM
If I understand correctly what you are asking you just need to remove the ~. So "~enemylayer" should be just "enemylayer"
@Adamcbrz thanks! and how would you print something (`print("hello");`) when there is a collision?
Debug.Log("collision: " + hit.collider.name);
This should go inside your if(Physics.Raycast(...))
@Adamcbrz than you! if i may : when should i use OnCollisionEnter over Linecast?
The short answer is it depends which one gives you the most useful information with the least overhead. Some times it's cheaper to Cast for a particular layer then to check every collision entering. It all depends on your application.
@Adamcbrz Ok i got it! Can i ask you a last question: is there a way to use the raycast with a direction of 360 degree? So far, i have to specify a single enemy, but i will have more than one rushing to the player : `if(Physics.Raycast(transform.position,enemy.transform.position,$$anonymous$$athf.Infinity, hit,enemyLayer.value))` ?
Your answer
Follow this Question
Related Questions
Enemy line of sight using linecast and colliders 1 Answer
Problems using an or (II) inside of an if statement. 1 Answer
Linecast not returning distance 2 Answers
Linecast ignoring mesh collider 1 Answer
Need Help Using LinecastNonAlloc 2 Answers