The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

What i want is that in buildMode the collision of the Building do not affect other shapes. Only be a visible Sprite to show where the Building can placed. If the building is placed then it needs the collision back again to prevent players to run through it.

My nodes:



Node2D [Main]

TileMap [Map]

KinematicBody2D [Player]

Sprite [Image]
Camera2D
CollisionPolygon2D [Collision]

Node2D [Buidlings]

KinematicBody2D [Building01]

Sprite [Image]
CollisionPolygon2D [Collision]


Szenario (short to explain):

# Variables
var allowPlace = false 
var buildMode = false

# on BuildMode Start:
buildMode = true
Building01.get_node("collision").set_trigger(true)

# on Fixed Process:
if buildMode:
    Building01.move_to(get_global_mouse_pos())
    var b = Buidling01.get_node("Collision")
    # How to do that here in a right way to check overlapping or collision ?
    if (b.is_colliding() || b.get_overlapping_bodies().size() > 0):
        allowPlace : false
    else:
        allowPlace: true

# on Mouseclick
if allowPlace:
    buildMode = false
    Building01.get_node("Collision").set_trigger(false)
else:
    print("OMG! You cant place a building on a Player or a collision map tile!")


Well I tried to use an Area2D instead of a KinematicBody2D for the Building01. There i can check if the Building01 is over another Shape but then i still have no collission when it is placed. That means my Player can run through the Building (although is_trigger equals false when placed).

If i use a KinematicBody2D then i cant really check if the Building is acutally over a Player or StaticBody2D with collision (while buildMode) but here i have the collision what prevents the player to run through the building.

I also tried to use a KinematicBody2D AND the Area2D in the [Building01] Node. But then they detect itselft as overlapping shape what will always return the wrong value for allowPlace.

I´m really confused how to fix that problem. Can anyone help me here ?

in Engine by (42 points)
edited by

1 Answer

+1 vote
Best answer

I got a solution which works fine for me.

Create the following scene as your Building:

Node2D
- StaticBody2D [Housecollisison]
-- CollisionPolygon2D [Option Trigger set to true]
- Area2D [Check overlapping]
-- CollisionPolygon2D

Then after you instance your Buildingsscene you have to attach that building to the mousepointer until you click LMB or RMB.

While the building ist following your cursor you have to check if the Area2D from the building have overlapping bodies on it. That will always return true because of its own StaticBody2D in it. So you have to get a list of these overlapping bodies by calling

area2D_node.get_overlapping_bodies()

then you have to get alle the childs of your building scene and remove the Area2D from the array (array.erase()) then check if the overlapping bodies == the Childs of your building (Area2D erased)
If it return true then you have no other collisison and can place the building by deactivate the building from following the cursor and set the "Trigger" option in the staticBody2D -> CollisionPolygon2D to false.

Greetings

by (42 points)
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.