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. Dismiss Notice

How to make RayCast ignore a specific tag?

Discussion in 'Scripting' started by zhuchun, Jun 5, 2014.

  1. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    423
    Hi, I'm using raycast to let the player know whether he has hit a enemy in my game.
    raycast.jpg

    Code (CSharp):
    1. RaycastHit hit;
    2.     Ray ray = new Ray (startPoint, direction);
    3.     bool isHit = Physics.Raycast (ray, out hit, rayLength);
    4.           if (isHit && hit.collider.tag == EnemyTag)) {
    5.                //bull's eye!
    6.     }
    I want the bullet can hit a enemy who stand behind his illusion, but the problem is that Raycast seems only return the first object it hits(via RaycastHit). Is that anyway to make Raycast ignore the illusion tag? Thanks!
     
  2. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    423
    I just realize there's a RaycastAll() could be the key
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It would be easier if you used layers instead of tags, since raycasts can easily ignore layers.

    --Eric
     
    PersianKiller likes this.
  4. zhuchun

    zhuchun

    Joined:
    Aug 11, 2012
    Posts:
    423
    Oh, I will give it a try :)