Spawning multiple KinematicBody2Ds on each other causes them to go flying across the screen at high speeds?

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

Hi there. I’m working on an enemy spawning system and essentially randomly generating x and y positions at the edges of the screen. The problem I’m seeing is occasionally the spawned enemies go flying across the screen at high speeds which is not what I want.

I believe this is because the randomly chosen spawn point of any given enemy can overlap with other enemy spawn positions. And because of that, move_and_slide is pushing the enemies out of each others CollisionShape2D at high speeds. The spawned enemies are all KinematicBody2D objects.

The relevant _physics_process() code is as follows:

var vec_to_player = player.global_position - global_position
vec_to_player = vec_to_player.normalized()
var v = move_and_slide(vec_to_player * MOTION_SPEED, Vector2.UP, false, 4, PI/4, false)

I think an easy fix for this might be to just set a max speed on my enemy but I’m not exactly sure how to go about doing that using move_and_slide or move_and_collide.

Any tips would be appreciated or if I’m completely wrong on what’s causing this issue, some guidance would go a long way as well.

Thanks!

is there any collision code ?

Inces | 2022-04-02 12:43

:bust_in_silhouette: Reply From: unicornsoftwareinc

I seem to have mostly fixed this issue by tweaking (increasing) the Safe Margin property of the KinematicBody2D enemy nodes.