#Creating particles on a specific surface when a raycast passes through

1 messages · Page 1 of 1 (latest)

kindred sage
#

Ey, just wanted to poke around in here. Had a question about raycasting and hitscans, couldn't find anything related to my question here or anywhere else online. Basically I wanted to know the best way to create decals/particles when a hitscan weapon for example, shoots through something like water, and hits the surface below it (like an ocean floor) but creates splash particles at the surface of the water.

I've tried using two separate raycasts in the same script that determine which surfaces are hit, the above water example using layer masks, but it's tricky. The raycast which hits the water instantiates a particle when I am looking at the water and firing the weapon, at even when looking at above-water terrain. Sorry for the long post, just hope there's a better solution out there of if I'm missing something, help could be provided

(I think this would also help with firing a weapon through glass for example)

tough dove
#

I'd still use 2 raycasts

#

One for the water (create splash)

#

and one from the hitPoint in the same direction

#

with different layer filtering

kindred sage
#

Seems like the best option, yeah

tough dove
#

I dont think you can do this in a single raycast

kindred sage
#

Ok

tough dove
#

2 raycasts gives you more control

#

and it is not less-performant

kindred sage
#

Then the only thing to work on would be to have it so the particle only displays on the surface of the water, not anywhere the player is looking when the water is within the raycast as well

tough dove
#

i don't get it

kindred sage
#

I know, it's kinda hard to explain

tough dove
#

just instantiate the particles on the hitPoint with the water

kindred sage
#

Well, the hitpoint needs to hit two surfaces, basically

tough dove
#

hitpoint cant hit anything

#

it's a point your ray hit

kindred sage
#

Like when you fire a bullet into a pool of water, the bullet hits the surface and creates a splash, but also hits the surface underneath the water

tough dove
#

make 2 raycasts

kindred sage
#

Sorry, you're right

tough dove
#
RaycastHit waterHit;
RaycastHit surfaceBelowWaterHit;

if (Physics.Raycast(hitscanOrigin, hitscanDirection, out waterHit, maxDistance, waterLayerMask)) {
    Vector3 waterHitPoint = waterHit.point;
    InstantiateSplashParticles(waterHitPoint);

    if (Physics.Raycast(waterHitPoint + hitscanDirection * epsilon, hitscanDirection, out surfaceBelowWaterHit, maxDistance - epsilon, belowWaterLayerMask)) {
        Vector3 surfaceBelowWaterHitPoint = surfaceBelowWaterHit.point;
        InstantiateImpactParticles(surfaceBelowWaterHitPoint);
    }
}
#

want me to make you a step-by-step guide? 😄

kindred sage
#

Maybe lol

#

Ty tho, for responding so fast and all

tough dove
#
  1. Fire raycast in the aiming direction with water layer mask

  2. If you hit a object with a collider that has a water layer mask assigned, create splash particle at hit point. Inside the same if(where you hit the water), make another raycast in the same direction you were shooting but with under water layer mask

  3. if you hit an object with a collider that has a under water layer mask, create impact particles at hitPoint

#

1 raycast for water

#

then if you hit water, second raycast from water position in the firing direction for ground

kindred sage
#

Yes, almost what I was doing, didn't think about casting a new raycast where the first one hits. That's interesting

tough dove
#

well you can do it from weapon's fire point aswell

#

but there's no point to do that really

#

i also assume you have some kind of firing range

#

so you cannot shoot at 1 kilometer with a pistol

#

so you can also set the raycast distance based on your firing range

#

let's say that you have pistol, with 10 range

#

distance from your gun to water hit point is 6 let's say

#

so the second raycast you make from water would be 4 distance (10-6)

#

sorry if that's to complicated 😄

kindred sage
#

No you're fine

#

I don't think that would be the most ideal, but it's definitely an option. You're saying that once a bullet hits the surface, it sends another raycast into the water. But I'm tryna think about if the weapon range would indeed complicate things a bit

#

Regardless if whether the range is 100km, maybe it's fine, as the raycast will hit the water surface anyway

tough dove
#

as i said

#

just manipulate the second raycasts ray distance

#

based on weapon shooting range

kindred sage
#

Tyvm yessir/ma'am