Godot Version
v4.5.stable.mono.official [876b29033]
Question
I want to ask both questions at once, since they are related. I’m implementing a mechanic where player can bounce on mob’s head. I’m handling it by adding an Area2D at the top of the mob, with this script attached to it:
public partial class MobBounceZone : Area2D
{
public override void _Ready()
{
BodyEntered += OnBodyEntered;
BodyExited += OnBodyExited;
}
private void OnBodyEntered(Node2D body)
{
if (body is CharacterBody2D characterBody && characterBody.Velocity.Y > 0)
{
var mobBounceComponent = body.GetNodeOrNull<MobBounceComponent>("MobBounceComponent");
if (mobBounceComponent != null)
{
mobBounceComponent.WantToBounce = true;
}
}
}
private void OnBodyExited(Node2D body)
{
if (body is CharacterBody2D)
{
var mobBounceComponent = body.GetNodeOrNull<MobBounceComponent>("MobBounceComponent");
if (mobBounceComponent != null)
{
mobBounceComponent.WantToBounce = false;
}
}
}
}
And in theory, it works. Player script is properly informed and triggers jumping. Player script currently has jumping, horizontal movement, and gravity methods called on loop in _PhysicsProcess. If WantToBounce is true, player is allowed to jump. This information might be related to issue number two.
This implementation has two issues:
- Checking for velocity is not enough, since player can still enter the Area2D from the side while falling, and it will incorrectly trigger the jump.
- There is a significant delay in triggering the jump, and player character falls below the
MobBounceZone, and enters the damage hitbox. It works flawlessly on low timescale, but not in real time.