Search Unity

Question Make RayCast Ignore Player & Gun

Discussion in 'Scripting' started by Mr_Packer12, Apr 20, 2023.

  1. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    I am looking for help on my FPS project. I have a shooting system that uses RayCasting. It comes from the FPS cam which is inside the player. It works but occasionally will hit either the gun or my arms if I move certain directions/ways. I have changed the point of origin to a separate point that is just beyond the camera, but moving that too far out to avoid hitting the gun and arms creates its own problem. How do I make the raycast simply ignore the gun and arms. I have tried numerous methods I have found, the "best" one seeming to be to add a LayerMask to the Physics.Raycast(), however that does not work. The arms are on their own layer as are the guns, yet any layermask I select in the inspector outside of "Nothing", the gun and arms will be hit. I have changed the settings in the unity project settings as one video suggested, that does nothing either. Any feedback would be great, thank you. Code Below:


    Code (CSharp):
    1.  
    2.  
    3. RaycastHit hit;
    4.  
    5. if (Physics.Raycast(bulletPointOfOrigin.transform.position, bulletPointOfOrigin.transform.forward, out hit, layerMask))
    6.         {
    7.             Debug.Log(hit.transform.name);
    8.              hit.transform.SendMessage("HitByRay");
    9.            
    10.             Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    11.  
    12.          
    13.                   if(hit.transform.tag != "Glass")
    14.             {
    15.                 Instantiate(bulletHole, hit.point, Quaternion.LookRotation(hit.normal));
    16.             }
    17.  
    18.             if(hit.transform.tag == "Glass")
    19.             {
    20.                 Instantiate(glassBulletHole, hit.point, Quaternion.LookRotation(hit.normal));
    21.                 FindObjectOfType<AudioManager>().Play("GlassShot");
    22.             }
    23.             if (hit.transform.tag =="ExplosiveBarrel")
    24.             {
    25.                 Instantiate(barrelExplosion, hit.point, Quaternion.LookRotation(hit.normal));
    26.                 FindObjectOfType<AudioManager>().Play("BarrelExplode");
    27.             }
    28.  
    29.             if (hit.rigidbody != null)
    30.             {
    31.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    32.             }
    33.        
    34.              
    35.         }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    You're not using it right. Check the documentation, you're putting your layermask in the parameter for "maxDistance".
     
  3. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    Oh jeez that was simple, my bad. Thank you!
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Yes it is confusing because both params accepts number so it won't throw compile error..
     
    mopthrow likes this.
  5. Mr_Packer12

    Mr_Packer12

    Joined:
    Aug 11, 2022
    Posts:
    30
    Yeah true
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Personally I got into the habit of using named arguments for things that have overloads or multiple same typed arguments such as this. It's not for everyone but it certain can be useful.
     
    Mr_Packer12 and Kurt-Dekker like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,701
    Agreed. I think it is critical for any function that either:

    a) has multiple arguments of the same type
    b) has multiple overloads that change argument order.

    Failure to use named arguments in the above cases is simply setting yourself up for failure. This is not a good strategy.

    Always use named arguments with
    Physics.Raycast()
    because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392
     
    Mr_Packer12 likes this.