0 votes

I want to implement "sliding" in my 2d game. So I wanna change collisions during sliding.
Now I simply create bunch of CollisionPolygon2D nodes and enable them manually in state machine. But this maks player node messy and unmanageable( Cuz all CollisionPolygon2D nodes have to be direct child of that KinematicsBody2D node).
Is there a more elegent way to manange collisions?

Godot version latest
in Engine by (14 points)

1 Answer

+1 vote

if you want to clean up your Player's node tree, you can make each CollisionPolygon2D an instance/scene, organize them in a specific folder and even add a script to attach individual properties to it.

Then you load and unload them dynamically in your player scene.

In my case, I created several different CollisionShapes for my vehicle, each attached with their own script that contains different property values.

Example:

body00.tscn w/ script:

extends CollisionShape2D
var speed = 100

And
body01.tscn w/ script:

extends CollisionShape2D
var speed = 150

Then on my players scene, in it's script, I dynamically load which vehicle body I need, and load their individual properties (such as speed), since they all use the same movement code.

player.tscn w/ script:

extends KinematicBody2D
array = ["res://body00.tscn", "res://body01.tscn", etc...]

func load_body(id)
 var pck = load(array[id])
 var body = pck.instance()
 speed = body.speed
 add_child(body)

Something like that.

by (334 points)
edited by

That's some how inspiring, many thanks!

glad to help, I honestly just stopped by here to take a break. Building the UI is such a chore, I got burned out a little bit. And yeah, I tend to digress when talking so I'll leave it at that. Don't hesitate to ask

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.