I have problem when i have too many KinematicBody3D into my scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Giannis1996

I have problem when i have too many KinematicBody3D into my scene
the computer is starting lagging when i have like 100 KinematicBody3D into the scene so…is there any lighter alternative to KinematicBody3D or is there anything else that i can do?

I need a huge number like up to 10000 KinematicBody3D so, if there is any solution or alternative to KinematicBody3D let me know!

The only thing that these bodies will do is to move in only 1 direction and disappear when they touch somewhere…there must be a far lighter alternative to KinematicBody3D

So, what’s your suggestion?

:bust_in_silhouette: Reply From: 1izNoob

There are broadly speaking 3 different physicsBodies:

KinematicBody for objects moving themselves, RigidBody for objects that can be influenced by outside forces and staticBody, which is not supposed to move at all.

I am not sure what your goal is in your game, but StaticBodys, for example, need less processing power than a KinematicBody.

:bust_in_silhouette: Reply From: Magso

As 1izNoob is saying, there’s not really an alternative node to use. You’ll need some kind of culling system that uses the VisibilityNotifier and the distance to the camera’s far clip pane to have add/remove child cull the kinematicbodies, however once removed only set_translation will be able to move them via an autoload or parent script.

The script would have to look something like this.

var kinematicbodies : Array = []#for all the kinematicbodies
var 
#optional player and camera vars

for i in kinematicbodies.size():
    if !kinematicbodies[i].get_node("VisibilityNotifier").is_on_screen() and player.get_translation().distance_to(kinematicbodies[i].get_translation()) > camera.far:
        if kinematicbodies[i].get_index() != -1:
            kinematicbodies[i].get_parent().remove_child(kinematicbodies[i])
        kinematicbodies[i].set_translation(get_translation() + Vector3(0, 0, speed))
    else:
        if kinematicbodies[i].get_index() == -1:
            kinematicbodies[i].get_parent().add_child(kinematicbodies[i])
        #use move_and_slide