It sounds like you’re encountering two separate issues:
Player blood effect appearing on enemies
This could be due to incorrect collision handling or blood effects being mistakenly parented to the wrong object.
Bat flying too low
The bat’s position or flight height may not be set correctly in its movement logic.
1. Fixing Blood Effect on Enemies:
Check Particle or Blood Effect Instantiation:
Ensure the blood effect is correctly linked to the player, not the enemy.
Correct Parent Assignment:
When creating the blood effect, make sure it’s instantiated at the correct position and parented properly:
void TakeDamage(int damage)
{
health -= damage;
// Instantiate blood effect at player's position, not enemy
GameObject blood = Instantiate(bloodEffectPrefab, transform.position, Quaternion.identity);
blood.transform.SetParent(null); // Ensures it doesn't parent to enemy accidentally
if (health <= 0)
{
Die();
}
}
Layer/Tag Checking:
Verify your collision detection differentiates between the player and enemies using tags or layers:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
// Only apply blood effect or damage logic to the player
}
}
2. Adjusting the Bat’s Flying Height:
Check Bat Movement Logic:
Adjust Vertical Position:
Ensure that the bat maintains a specific Y-axis value or follows a predefined height range:
public float flightHeight = 3.0f; // Desired height
public float speed = 2.0f;
void Update()
{
Vector3 targetPosition = player.position; // Assume bat chases player
targetPosition.y = flightHeight; // Set fixed height
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
}
Pathfinding Check:
If you’re using pathfinding or waypoints, ensure they are set at the desired height.
Collision with Ground:
The bat may collide with the ground or objects. Ensure its Collider is set to avoid ground collisions by:
Adjusting the bat’s collider height or offset.
Checking collision layers to avoid ground objects.
Debugging Tips:
Logs and Debugging: Add Debug.Log() messages in both the blood and bat scripts to ensure they’re being called and positioned correctly.