fixed velocity handling for bounciness

This commit is contained in:
go38coz
2026-06-06 16:05:21 +02:00
parent f944111a52
commit bfd0ce923c
+19 -5
View File
@@ -4,6 +4,7 @@ using Object = System.Object;
public class DamageHandler : MonoBehaviour public class DamageHandler : MonoBehaviour
{ {
private Rigidbody2D rb;
public float threshold = 2.5f; public float threshold = 2.5f;
public float multiplier = 0.5f; public float multiplier = 0.5f;
@@ -11,23 +12,35 @@ public class DamageHandler : MonoBehaviour
public float health; public float health;
public float minSize = 0.4f; public float minSize = 0.4f;
private float lastYVelocity;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Start() private void Start()
{ {
threshold = 2.5f; threshold = 10f;
multiplier = 0.5f; multiplier = 0.1f;
maxHealth = 1f; maxHealth = 1f;
minSize = 0.8f; minSize = 0.8f;
health = maxHealth; health = maxHealth;
} }
private void FixedUpdate()
{
lastYVelocity = rb.linearVelocityY;
}
private void OnCollisionEnter2D(Collision2D other) private void OnCollisionEnter2D(Collision2D other)
{ {
float fallSpeed = GetComponent<Rigidbody2D>().linearVelocityY; float fallSpeed = lastYVelocity;
if (fallSpeed > threshold) if (fallSpeed < -threshold)
{ {
float fallDamage = (fallSpeed - threshold) * multiplier; float fallDamage = (-fallSpeed - threshold) * multiplier;
Debug.Log($"Fell with velocity:{fallSpeed}. Fall damage{fallDamage}"); Debug.Log($"Fell with velocity:{fallSpeed}. Fall damage{fallDamage}");
UpdateHealth(health - fallDamage); UpdateHealth(health - fallDamage);
// Debug.Log($"New health:{health - fallDamage}"); // Debug.Log($"New health:{health - fallDamage}");
@@ -38,6 +51,7 @@ public class DamageHandler : MonoBehaviour
} }
} }
public void UpdateHealth(float health) public void UpdateHealth(float health)
{ {
this.health = health; this.health = health;