Well, I’ve a camera tagged as a Main Camera and in front of it a particle system of bubbles. I attached a script to the camera, but unfortunately the ray pass through the particles and only hits the terrain. Here’s the code:
var bollaEsplosa : GameObject; // Particella da creare quando una bolla esplode
var layermask : LayerMask;
function Update () {
if (Input.GetButtonDown("Fire1")){ // se si preme il pulsante sinistro del mouse
var hit : RaycastHit;//variabile nella quale sono contenute le informazioni della collisione
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);//creiamo un raggio che parte dalla camera basandoci sulla posizione del mouse (metti come tag della camera "MAIN CAMERA")
if (Physics.Raycast(ray.origin,ray.direction, hit, Mathf.Infinity, layermask)){//Se il raggio colpisce qualcosa
Instantiate(bollaEsplosa,hit.point, Quaternion.identity);
}
Debug.Log(ray.direction);
}
}
There’s no easy way of doing this unfortunately. Instead, a workaround of creating your own emitter with a timer and instantiation of bubble prefabs would be much easier. You could store the instantiated objects into an array and let that array take care of the behaviors (such as movement, billboarding and lifetime). Raycasting would then be fully available onto the bubble mesh (which could be an image on a single plane).
If i understand it, you want a ray to not pass trough the particles, but collide with them. If So just put a collider, on it and click Is Trigger, it will now detect collision, but you can still pass trough. So your code would be something like
var explodedBubble : GameObject;
var layermask : LayerMask;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
var ray:Ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin,ray.direction, hit, Mathf.Infinity, layermask))
{
Instantiate(explodedBubble, hit.point, Quaternion.identity);
}
// Debug.Log(ray.direction);
}
}