Search Unity

Filter Raycasting by Faction

Discussion in 'Scripting' started by Afropenguinn, Oct 22, 2014.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Before you read, just know this is not a problem for me as much as it is a curiosity, I am just looking to learn something new! :)

    So I have 2 factions in my game, and each of them shoots the same bullet class. However, each bullet has a variable stating which faction that bullet should hit. Then I simply put both factions in a separate layer and used that to filter the raycast. This worked for my purposes, but then that raised the question, is there a better way? Is there a way to achieve the same results with similar performance without using layers? I raised this question because if you had more than a handful of factions you would begin to run out of layers fast. So can you think of a solution?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    With only two factions that's probably the way I would do it. Otherwise, you could put all units on a single layer and check a tag or put an Identity component on each unit and check some faction value in that Identity instance.
     
  3. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I had thought of that as well, in fact that is what I tried first! The problem with that is:
    Let's say we have a raycast that is of length 20, a character of length 10, a bullet that is 5 away from the character, and a wall behind the character, The bullet checks ahead with its length of 20, it intersects the character and the wall, though it only returns the character because it was the first thing hit! If the character is in the faction it is ignoring it will ignores that collision and appear on the other side of the wall and keeps going.

    Edit: Well now I feel like a fool! There is a function called RaycastAll that fixes this problem! Would this method be any more efficient than layers or less so?
     
    Last edited: Oct 22, 2014
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You can (and should) still use layers because it speeds up the operation of the raycast itself. Typically, you would only raycast in front of the bullet the distance the bullet will travel in the frame to determine if it would intersect anything during that movement so RaycastAll wouldn't be necessary because you would only want the closest thing - however, this would not be the case if your bullets traveled instantly.
     
  5. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Wouldn't you still need RaycastAll though because of the example I just gave? A bullet travels at a speed of 20, its ray hits a character and a wall (hitting the character first). It then checks if it should ignore that character. It says it should, so it just moves forward in stead as if nothing was there. The problem is: we also hit a wall but it was never returned since the character was in the way. So the next frame it will either appear inside the wall or on the other side of the wall, since it doesn't know the point of contact of the wall.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Yeah I suppose. Either way...
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Instead of using a layermask for 'only the enemy faction' use a mask for 'everything except friendlies'
    that way the raycast will hit walls and enemy players