Directional particle splatter

Hey guys!

I’m trying to make the blood splatter in the direction that the player attacks, like in the below picture:

I’ve managed to get a solution but wanted to ask if it’s the right way to go about it! I feel like I’m on the right lines but not sure if I’m calculating the angle in the right wat. Here’s what I’ve done:

  • Changed the shape of the particle system to a cone
  • Updated the rotation of the particle system to x: -90 (so the cone faces in the 2D space, rather than facing the camera)
  • When the particle system is created, get the angle between the player and enemy then rotate the Y axis of the particle system shape property

Here’s the code that is part of the EnemyHealth class.

private void DetectDeath()
{
    if(currentHealth <= 0)
    {
        // create the particle effect prefab and get the ParticleSystem
        ParticleSystem obj = Instantiate(deathVFXPrefab, 
            transform.position, Quaternion.Euler(new Vector3(-90,0,0))
            ).GetComponent<ParticleSystem>();

        // Get the shape property of the particle system
        var shape = obj.shape;

        // Calculate the rotation between where the player is and where the enemy is
        var angleY = Vector3.Angle(PlayerController.Instance.transform.position, this.transform.position);
            
        // Apply the angle on the Y axis to the shape
        shape.rotation = new Vector3(0, angleY, 0);

        Destroy(gameObject);
    }
}
1 Like

There’s certainly more than one way to do things – your solution is great! I actually had the same idea for my particle system, but I did mine a little different because I was lazy and wanted to avoid editing its shape. Instead, I manipulated its velocityOverLifetime and set that to be in the direction the damage was dealt, multiplied by the particle system’s startSpeed.

public void SetParticleVelocity(DamageSource damageSource)
{
    Vector2 damageDirection =
        (transform.position - damageSource.Instigator.position).normalized *
        _particleSystem.main.startSpeed.constant;

    ParticleSystem.VelocityOverLifetimeModule particleVelocity = _particleSystem.velocityOverLifetime;

    particleVelocity.enabled = true;
    particleVelocity.space = ParticleSystemSimulationSpace.Local;
    particleVelocity.x = damageDirection.x;
    particleVelocity.y = damageDirection.y;
}
2 Likes

Ah ha, nice solution :ok_hand: I’ve given that a blast and it gives a nice effect, thanks very much for sharing.

1 Like

As @tfedoris said, there are always multiple ways to do things. This solution is an excellent one! Straight to the point and gets the job done. Well done!

1 Like

Privacy & Terms