How can i make through shootable surfaces with raycast?

sorry i don’t know to understand you the quastion

I use this script to shoot and i want to shoot through some object help

function Shoot(){

if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
var DirectionRay = SprayDirection();
CanFire = false;
Ammo -=1;

Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
    if(Physics.Raycast(transform.position, DirectionRay, hit, Range)){
    // prepare rotation to instantiate blood or sparks
    var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
    if (hit.transform.tag == "Enemy"){ // if enemy hit...
        if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
        CrossHair.renderer.material.SetColor ("_Color", Color.red);
        hit.transform.SendMessage("ApplyDamage", 10, SendMessageOptions.DontRequireReceiver); // and consume its health
        
    }
      if (hit.transform.tag == "Target"){ // if enemy hit...
        if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot); // make it bleed...
        CrossHair.SetActiveRecursively(false);
        CrossHairHit.SetActiveRecursively(true);
        hit.transform.SendMessage("ApplyDamage", 10, SendMessageOptions.DontRequireReceiver); // and consume its health
        
        
        }
        
        
        
        
       if (hit.transform.tag == "Wood"){// otherwise emit sparks at the hit point
        if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
    }
    if (hit.transform.tag == "Iron"){ // if enemy hit...
        if (ironPrefab) Instantiate(ironPrefab, hit.point, rot);
        }
        if (hit.transform.tag == "Glass"){ // if enemy hit...
        if (glassPrefab) Instantiate(glassPrefab, hit.point, rot); // make it bleed...
        hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health
}
if (hit.transform.tag == "Terrain"){ // if enemy hit...
        if (terrainPrefab) Instantiate(terrainPrefab, hit.point, rot);
        }
        if (hit.transform.tag == "Water"){ // if enemy hit...
        if (waterPrefab) Instantiate(waterPrefab, hit.point, rot);
        }

}
yield WaitForSeconds(FireRate);
CanFire = true;

}

If you want to shoot through some of the gameobjects, look at raycast mask.

You can use a mask in the raycast to selectively ignore one or few layers, if a gameobject/collider belongs to this layer, it will not interact/interfere with the raycast (so you sort of shoot through the gameobject). http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html

@alucardj, your comment disappear from here but I can see it in my mail inbox. That's a interesting competition, I supposed that there is no limit to what sort of tool I can use in the competition? I will keep an eye on the website and I might participate the next competition.

Thanks! :)

1 Answer

1

(All of this code is untested and simply improvised)
If you want a ray to register hits until it travels a certain distance:

var hit : RaycastHit;
var rangeMax = 100.0f;
var rangeTraveled = 0.0f;
var rangeRemaining = 100.0f;
var from : Vector3;
var direction : Vector3;

while(rangeTraveled < rangeMax)
{

    if(Physics.RayCast(from, direction, hit, rangeRemaining))
    {

        Debug.Log("Hit collider '" + hit.collider + "' at point: " + hit.point);

        rangeRemaining -= hit.distance;
        rangeTraveled += hit.distance;

    }
    else
    {

        rangeTraveled = rangeMax;

    }

}

Btw. If you want to be able to shoot through multiple gameobjects but still deal damage or instantiate bullet holes when you hit these gameobject you can use Physics.RaycastAll. This will return an array of hits as opposed to a single one like the normal Raycast. Script Reference: http://docs.unity3d.com/Documentation/ScriptReference/Physics.RaycastAll.html . Hope this helps.