0 votes

I am making a SHMUP style game with asteroids and a main player ship. When the player kills an asteroid, multiple can spawn where the old one was. It appears that if a new asteroid spawns inside the player, he will teleport or just be instantly destroyed. Both entities move via move_and_slide. Asteroid uses a circle CollisionShape2D, ship is a CollisionPolygon2D.

How can I prevent this wonky player ship behavior? The only thing I can think of is checking position to ensure asteroids are not on top of the player, but that seems like it would be a lot of collision checking, tedious/fragile code.

in Engine by (678 points)

1 Answer

0 votes

KinematicBody2D will try to avoid collision. So if you move it by moveandslide, it will move away from the collider if it collides. If enough force is involved, this could look like teleportation, which really just is very fast movement in this case. To avoid this, set the CollisionMask and Layer of the new asteroid so, that it won't collide with the player for a fixed amount of time and have it snap back to normal after some time.

by (840 points)

Good idea! But what if they are overlapping when the mask/layer snap back to normal? Wouldn't this cause the same problem?

using move_and_collide instead of move_and_slide fixes the issue, like you suggested it might. I think I will go with this solution for now because of simplicity/robustness, even though it's not quite the effect I'm after. I'm still interested in getting a working solution for the original problem though.

Also, I believe the player was being destroyed because he was being moved/teleported outside the viewport, which triggers code to call queue_free

Is there any way to set a maximum force/speed on a kinematic body?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.