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

Raycast ignore objects ?

Discussion in 'Scripting' started by Quast, Dec 21, 2016.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Here the raycast went out from camera. What i need is make it ignore hitting any object between camera and player. I got the distance between them but i don't know how to make it ignore it ?
    Code (CSharp):
    1. void FixedUpdate()     {
    2.         Vector3 fwd = transform.TransformDirection(Vector3.forward);
    3.         // the distance between camera and player
    4.         float cam_player_ignore = Vector3.Distance(gameObject.transform.position, player.transform.position);
    5.  
    6.         RaycastHit hit;
    7.         if (Physics.Raycast(transform.position, fwd, out hit,100)) {
    8.             print("Ray hit something !");
    9.             // here !!!
    10.         }
    11.  
    12.         else {
    13.             print (" Nothing ");
    14.         }
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Edit>Project Settings>Physics
    Have you tried the layer matrix? You can use that to let raycast ignore certain layers.
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I'm not looking for layers here.
    I need every thing that come up between camera and player will ignored by the raycast.
     
  4. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Can you explain what you are trying to achieve, then we can maybe help. Is it a bullet shooting?
     
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Yes, the raycast went from the main camera and at end there is a crosshair image.
    All thing is good until something between the camera and the player blocking the raycast. this thing could be enemy, wall, tree and whatever. To avoid this problem, i calculated the distance that i need the raycast to ignore it.
    But i don't know how to do it.
     
  6. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    Sorry, I am not sure if a ray will continue once it hit a collider. That is why I say layers might be your solution.
     
  7. ShvetsJR

    ShvetsJR

    Joined:
    Jun 5, 2019
    Posts:
    3
    Did you find any solution? Experiencing same issue
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Use a LayerMask to ignore certain layers from your raycast.
     
  9. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    If you are not willing to use layers, which is the goto approach for this thing..

    You can do a raycast against all objects hit, then filter out the ones you want to ignore. Either by tag name or other means.

    You can use link to help you with the filtering also.. Something like this:
    Code (CSharp):
    1.  
    2. using linq;
    3. hits = hits.where(x => x.tag != "player").ToArray();
    4.  

    https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html
     
    testsymbol likes this.