Managing sounds, particles, etc

Just wondering how do people manage this stuff.

Let’s take a basic example. Player takes damage. We want to play a sound and some kind of particle that shows damage was taken. What is the best way to control this? Inside the Player component? Or have a separate classes for sounds, particles, etc.

You’ll could drive this by the “bullet”. Data components being put on the “things being hit”.

So when the projectile hits something is says “hi thing I just hit, if you have the ‘something hit you’ component; what particle effect do you make when I hit you, what sound do you make? … cool i’ll use those my way”

Or you can just have your “bullet” say “I hit you, do your thing” and have a component on the thing being hit handle all the effects taking the impact point / direction from the bullet

Well, currently I have main Player script which has various components inside it, such as Health, Mana, etc. These components handle their own state independant of the main Player script.

This Player script has a public method DealDamage(int value). This method is called by the GameManager class when it detects that Player should take damage.

However, Player can also block damage. Depending whether damage was taken or blocked, I display a different particle and play a different sound.

There’s also various other effects that can take place. Player can use heal effect which has its own stuff, etc. Should I call create these effects and play sounds inside the Player script or should an outside class handle those things?

For example, I could add another component to Player - PlayerSFX and this component would have prefabs for AudioClips and ParticleSystems.

what is the logic to the “is this blocked”?

if the dealdamage function does some checking and then calls a function on the healthscript or blockscript I’d probably put the effect handling on the health and block scripts. You can always pass along more parameters.

so

// more psuedocode than real code ;)

void DealDamage(float d, Vector3 hitPoint, Vector3 hitDirection)
{
    if(canBeBlocked)
    {
        blockscript.Block(hitPoint, hitDirection);
    }
    else
    {
        healthScript.TakeHit(d, hitPoint, hitDirection);
    }
}


void healthScript.TakeHit(float d, Vector3 hitPoint, Vector3 hitDirection)
{
    Instantiate(bloodsprayPrefab, hitPoint, Quaternion.SetFromToRotation(transform.forward, hitDirection));
}


void blockscript.Block(Vector3 hitPoint, Vector3 hitDirection)
{
    Instantiate(blockEffectPrefab, hitPoint, Quaternion.SetFromToRotation(transform.forward, -hitDirection));
}

if you want different blockeffects for different weapons you can always extend the parameters and send the weapon type along etc.

Ok, thanks. My logic for blocking is pretty much what you have there, but instead I had a Particle / Sound Manager classes. Then I’d call soundManager.PlayThisOrThatSound(). But I think it is better to have that as components inside my Player object.