0 votes

I have three instances in my scene. Each of these instances are different sizes and I was trying to programmatically resize the CollisionShape2D for each instance. However, I noticed that all the instances share the same size as the last instance in my scene.

I am using the following code to change the CollisionShape2D in the instance node.

get_node("Area2D/CollisionShape2D").get_shape().set_extents(Vector2(scale * 10.0, 5.0))

But this seems to target all the CollisionShape2Ds in the scene, rather than just the specific instance to which it is attached.

How do I target the specific instance in this case?

in Engine by (18 points)

If you duplicated the instances after assigning the collision shape they share the same collision shape, manually create a unique collision shape for every instance.

Thanks, that solved the problem.

1 Answer

+2 votes
Best answer

Remember that the CollisionShape2D nodes were created as helpers of the editor, so all the instances of the same node will share the same shape (as the shape is defined in the Physics server in runtime, and the node has no more purpose). Check this post, there you can see how to add a shape to a Area2D. Maybe is a better approach to add the collision in runtime rather than creating the CollisionShape2D node in the editor.

by (697 points)
selected by

Now that I look at the documentation, I did miss that it says its a editor only class.

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 Frequently asked questions and 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 [email protected] with your username.