Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Linecast that ignore it's self?

Discussion in 'Scripting' started by RaGEn, Jul 27, 2017.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Hello I'm currently making AI for my game. I need their line of sight broken when a friendly unit moves in front of them. However each AI has the same layer on them on them to break other friendlies line of sight. This in term makes the AI break their line of sight from themselves. I'm very new to layers and linecasts and I'm unaware of how to approach this problem.

    Does anyone have an idea how to prevent a linecast from seeing a layer attached to the gameobject or the gameobject's child that it is being casted from?

    This is my code I'm using.
    Code (CSharp):
    1.      
    2.     public LayerMask FriendlyLayer;
    3.     private bool FriendlyBlocked;
    4.  
    5.        
    6.  if (Physics.Linecast (Target.transform.position, transform.position, out hit, FriendlyLayer))
    7.                 {
    8.                         FriendlyBlocked = true;
    9.                         Debug.Log ("Hit");
    10.                 }
    11.                 else
    12.                 {
    13.  
    14.                         FriendlyBlocked = false;
    15.  
    16.                 }
    Thank you for any help that you can provide.
     
  2. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    I just made it so the linecast now shoots from in front of the AI. However if anyone knows another way I'd be happy to hear it! Since I just want to learn as much as I can.
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Mostly I just do the same thing and move the linecast so it starts in front of the source AI object.

    You can also change the layer of your AI (in code) before the linecast and change it back afterwards, that way you can fire the linecast without worrying about it hitting the object it's cast from.

    e.g.

    Code (CSharp):
    1. int DefaultLayer = LayerMask.NameToLayer( "Default" );
    2. int AILayer = LayerMask.NameToLayer( "AI" );
    3.  
    4. currentObject.layer = DefaultLayer;
    5.  
    6. // Do linecast
    7.  
    8. currentObject.layer = AILayer;
    9.  
     
  4. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    Thanks for the reply. That's an interesting idea, I'll keep that in mind as well. Thank you! :)