ray casting specific collider

HI,

Ive used the following script to create bullet holes in the walls when my gun fires and it works perfectly. My problem is that I have a character with it’s own bullet hole and death animation and I need this script to not instantiate a bullet hole when my character is hit. Only when the walls are hit, this script should be active. Does anyone know how to make that happen with this script?

var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes
function Update () {
   var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
   var hit : RaycastHit;
   Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters
if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something
         Instantiate(bulletTex[Random.Range(0,1)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
         }
         }

Check the collider game object’s name or tag.

Im relatively new to scripting so can you elaborate, what does “check” mean and how would I write it in javascript?

You can specify a layermask when raycasting.

1 Like

I’ve been trying to do as you say and use layer mask but it removes the instantiated bullet holes in the walls all together. Did I write it wrong? heres the updated code:

var bulletTex : GameObject[];
var Mylayermask : LayerMask;
function Update () {

  
   var fwd = transform.TransformDirection(Vector3.forward);
   var hit : RaycastHit;
   Debug.DrawRay(transform.position, fwd * 10, Color.green);
if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10, Mylayermask)){
         Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
         }
         }

Ive selected aloe the walls and layered them with “Mylayermask” but it doesn’t work :frowning:

You have to make Mylayermask in the inspector equal to the walls’. There should be a dropdown list.

1 Like

Do you mean the drop down in the top right corner called “Layers:”? because Ive selected Mylayermask for all the walls. and what do you mean with making Mylayermask “equal” to the walls?

I mean the LayerMask variable in your script. It should be showing in the inspector.

“Mylayermask” is just the name of your variable right now.

You’ve set the walls correctly so far, but you haven’t set the variable.

1 Like

YYYEEEEEEEEEEESSSS :smile:DDDD such a small little thing that I overlooked and now it all works, thank you very much for your help and advice!!! That’s Awesome!