0
\$\begingroup\$

I have a 3D side scroller style game in which the player flies along avoiding stuff, you know, side-scrollery things.

I'd like to add an effect (particle system) to the player when they get close (say 1.6 units (meters)) to the terrain (dangeZone), like a dusty dragging cloud under them sort of thing. I'm familiar with the particle system and raycasts but I don't know how to marry the concepts together to achieve what I'm after.

I'd also be hoping to make the particle system 'grow' the closer the player gets to the terrain if that makes sense. There is also speed to consider, so the closer to the ground and faster the player is should have an effect on the particle system.

My Thoughts:

I already have a score multiplier that uses a raycast to check the player's position from the ground/terrain and increases the closer they get.

void Update() {

    force = speed *2;

    RaycastHit hit;
    Ray downRay = new Ray(transform.position, -Vector3.up);

    if (Physics.Raycast(downRay, out hit, dangerZone)) {

        var distanceToGround = hit.distance;
        float hazardMultiplier = Mathf.Round( (transform.position.y - distanceToGround)*100 ) /100;

        if (hit.collider.tag == "Terrain") {
            playerData.scoreMulitplier = hazardMultiplier;
        }
    } else {
        playerData.scoreMulitplier = playerData.baseScoreMulitplier;
    }
}

I'm thinking I can use the raycast I already have to instantiate a particle system on the terrain/at the raycast hit point but I'm not sure how exactly to go about this.

Any help is appreciated and thanks in advance.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Baby steps. 1. Take the raycast code you already have working and put it into an empty scene -- get it working there in the simplest possible context. 2. Improve that so it can instantiate a cube at the raycast hit location. 3. Improve that so you can instantiate an empty particle system. 4. Make the particle system work. 5. Reintegrate all this back into your game code now that you understand how it works together. \$\endgroup\$ Commented May 18, 2022 at 15:58
  • \$\begingroup\$ Excellent advice, thank you. Could you tell me or direct me to how to instantiate at the end point of the raycast? \$\endgroup\$ Commented May 18, 2022 at 16:03
  • \$\begingroup\$ docs.unity3d.com/ScriptReference/Object.Instantiate.html \$\endgroup\$ Commented May 18, 2022 at 16:25
  • \$\begingroup\$ It does not look to me like you need help with instantiating a new particle system. You just need one instance, which can exist as part of the initial scene/vehicle prefab, and which you position each frame to be at hit.point, and adjust its emission parameters to make it more intense when close and pause emitting when too far. It's not clear to me how you've attempted to do this so far, where you've encountered trouble, and what help you need. \$\endgroup\$ Commented May 19, 2022 at 11:57
  • \$\begingroup\$ @DMGregory I haven't yet tried to instantiate the particles (PS) as I was unsure if hit.point was what I needed to use. I had thought it might be, but by the time I posted this question coding had turned my brain to mince for the day. So from what you're saying I should make 1 PS and just have that change it's features dependent on the distance to the ground? \$\endgroup\$ Commented May 19, 2022 at 12:05

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.