- Home /
How to raycast through friendly objects?
I have turrets on ships as seen here:
These turrets will rotate towards a target, but will not shoot at it till the turret is directly pointing at the enemy ship (any spot on the enemy ships collider). I do this so that turrets will not try and aim for the middle of an enemy before firing, and so that they only fire when facing the enemy. I accomplish this with a raycast. However, since all units in the scene (including enemy units) are on the same layer, I cannot ignore the Unit layer when doing the raycast (this is not changeable due to pathfinding). This causes the raycast to be blocked by allied units and they will not fire at the enemy (I still want them to shoot, even if the enemy is not within line of sight).
How would I go about doing this? RayCastAll seems to be a bit expensive performance wise if I was to have hundreds of units using it per frame, especially if there are a large number of units between the ones firing and the enemy.
Since you have them all on the same layer, you could loop through the enemies and use each one's collider to test collider.raycast to detect a hit on each. http://docs.unity3d.com/ScriptReference/Collider.Raycast.html
Ah, that's a pretty good solution. However that brings up another problem, how to make it hit some objects like buildings or environmental objects like asteroids...etc
You can add any gameobject with a collider to the list you check.
Answer by sparkzbarca · Apr 17, 2015 at 10:24 PM
you could add an empty game object as a child and add a collider to it and place that in another layer allowing you to basically have multiple layers on a single object as long as you make sure you get the transform.parent.
also the best solution is a small modification to your pathfinding code.
Layers for casting against can be AND/OR together
for example you can cast against layer 1 and 2 but as a single layer as it were.
what you should have is a allied layer and an enemy layer and in pathfinding as the layer do it as allied/enemy.
anytime you have a layer input you can use a a legal input the input
int | int
so
physics.raycast(...., layer 1 | layer 2) will work on either layer
alternatly
int Layer = 1<<4 | 1<<5;
is a a Layer now that works against either layer 4 or 5
Thanks for the reply. The pathfinding is based on Unit/Static Object/Ground. "enemy" is arbitrary since this will be a multiplayer game, ins$$anonymous$$d it will have Player1, Player2...etc
I could have a layer for each player, so that I can cast just against that layer. Ie Player 2,3 and 4 are enemies, I would cast against their respective layers.
As for the actual layer mask part of it. I found making a public field of type Layer$$anonymous$$ask, and assigning the layers in the inspector was much easier.
Having a child object with the other layer sounds like a good idea, I just need to make sure I'm not putting undue stress on the performance of the game with mesh colliders.
Your answer
Follow this Question
Related Questions
Raycast Layermask doesn't ignore no layered Parent? 1 Answer
How do i add a layer to a gameobject? 1 Answer
Make Certain Object's Raycasts Ignore Certain Colliders 0 Answers
Shoot through walls 1 Answer
Ignore clicks on all layers except one 0 Answers