Reputation: 31
I'm making a FPS game, and the player can shoot himself it he aims down. If I add a layermask, it's fixed, but since I plan on making this game multiplayer, every player would have that same layermask. I'd rather not add yet another layermask to other players, if that's possible. Is there some other way to avoid the player shooting himself, withot using layermasks? Or is there an actual way to make other players have a seperate layermask?
Upvotes: 1
Views: 211
Reputation: 90639
Or is there an actual way to make other players have a seperate layermask?
As Layers need to be defined beforehand and are limited to 32 layers (8 already predefined by Unity) this solution would be feasible to implement, yes, but not be very scalable and depending to the complexity of the rest of your app very limited.
You could use RaycastAll
, filter out if(hit != player)
e.g. using Linq Where
, order by the distance e.g. using Linq OrderBy
and then use the first hit from the results.
Somewhat like
// Wherever you get these from
Ray ray; // the ray with origin amd direction for the cast
float distance; // if needed a maximum distance
LayerMask layers; // the general raycast layer - not different ones for each player ;)
GameObject player; // your player object
var hits = Physics.RaycastAll(ray, distance, layers);
if(hits.Length > 0)
{
// Filter out e.g. using Linq
hits = hits.Where(hit => hit.gameObject != player).ToArray();
if(hits.Length > 1)
{
// Sort by distance from ray origin
hits = hits.OrderBy(hit => hit.distance).ToArray();
}
// Finally get first hit
var hit = hits[0];
// ...
}
Upvotes: 1