How to stop/stop animation when an object is not in the camera's field of view

Godot 4.2.0

Question

I am creating a 3D turn-based strategy game. I have a bunch of units with hitboxes of the head, arms, legs of the torso that are attached to the skeleton of the character. And when you play a character, part of the fps is eaten by the physics engine (this is understandable). The question is how to make the character get that he is not displayed in the camera and can pause the animation? It turns out that looped animation always works
Безымянный2

Hey there,

did you try to use the VisibleOnScreenNotifier3D ?
You can control whats happening when the object is displayed in the camera or not.

For example:

func _on_visible_on_screen_notifier_3d_screen_exited():
	$AnimationPlayer.stop()
        # Do all other stuff here to save performance

I just hope is not bugged like 2D version.
obraz
C#? GDSscript??

My example is GDscript.

image

i USE this method

foreach (var unit in Units)
{
	if (unit.IsDead) continue;
	if (Camera3DGround.IsPositionInFrustum(unit.Position))
		unit.VisibleByPlayer = true;
	else
		unit.VisibleByPlayer = false;
}

WHERE

    private bool _visibleByPlayer;
    public bool VisibleByPlayer
    {
	    get => _visibleByPlayer;
	    set
	    {
		    if (_visibleByPlayer == value) return;
		    _visibleByPlayer = value;
		    if (_visibleByPlayer)
		    {
			    _animationPlayer.Play(Animation.Get(AnimaPersonState.Idle3));
			    Show();
		    }
		    else
		    {
			   _animationPlayer.Stop();
			    Hide();
		    }
	    }
    }
1 Like

for CAMERA3D use VisibleOnScreenNotifier3D is no elegante

1 Like