Sure, here's an updated version of the script that makes the blood splat appear from the enemy's direction:
using UnityEngine;
public class BloodSplat : MonoBehaviour
{
public GameObject bloodSplatPrefab;
public float bloodAmount = 10.0f;
public float bloodSpeed = 5.0f;
public float bloodSize = 1.0f;
public Color bloodColor = Color.red;
public float bloodDuration = 1.0f;
public float bloodCooldown = 0.5f;
private float currentBlood = 0.0f;
private float currentTime = 0.0f;
void Update()
{
// Check if the player was hit
if (health.GetHealth() < health.GetMaxHealth())
{
// Create a new blood splat
currentBlood += bloodAmount;
currentTime = 0.0f;
// Instantiate the blood splat prefab
GameObject bloodSplat = Instantiate(bloodSplatPrefab, transform.position, Quaternion.identity);
// Set the blood splat's properties
bloodSplat.GetComponent<SpriteRenderer>().color = bloodColor;
bloodSplat.GetComponent<Rigidbody2D>().velocity = Vector2.right * bloodSpeed;
bloodSplat.GetComponent<Rigidbody2D>().mass = bloodSize;
// Add the blood splat to the list of blood splats
bloodSplats.Add(bloodSplat);
}
// Update the current time since the last blood splat
currentTime += Time.deltaTime;
// Check if the current time is greater than the blood cooldown
if (currentTime > bloodCooldown)
{
// Reset the current time
currentTime = 0.0f;
// Check if the blood splat list is empty
if (bloodSplats.Count > 0)
{
// Remove the oldest blood splat from the list
bloodSplats.RemoveAt(0);
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
// Check if the player was hit
if (collision.gameObject.name == "Player")
{
// Create a new blood splat
currentBlood += bloodAmount;
currentTime = 0.0f;
// Instantiate the blood splat prefab
GameObject bloodSplat = Instantiate(bloodSplatPrefab, transform.position, Quaternion.identity);
// Set the blood splat's properties
bloodSplat.GetComponent<SpriteRenderer>().color = bloodColor;
bloodSplat.GetComponent<Rigidbody2D>().velocity = Vector2.right * bloodSpeed;
bloodSplat.GetComponent<Rigidbody2D>().mass = bloodSize;
// Add the blood splat to the list of blood splats
bloodSplats.Add(bloodSplat);
}
}
}
In this updated version, we've added a new OnCollisionEnter2D method that is called whenever the player collides with an enemy. In this method, we create a new blood splat and set its properties, just like in the Update method.
We've also changed the bloodSplat.GetComponent<Rigidbody2D>().velocity line to to make the blood splat move in the direction of the enemy.