#static body disable when instantiate another one

14 messages · Page 1 of 1 (latest)

limber saffron
#

Hello, I created a simple script to instantiate wall where the player has click. It work fine but I don't know why, when I instantiate 2 or more walls, the only one my player can collide with is the last one. Does anyone have an idea ?

func _input(event):
    if (event is InputEventMouseButton
    && event.button_index == MOUSE_BUTTON_LEFT
    && event.pressed):
        if (firstVertex == Vector2.ZERO):
            firstVertex = event.position
        else:
            var secondVertex: Vector2 = event.position
            initWall(firstVertex, secondVertex)
            firstVertex = Vector2.ZERO

func initWall(firstVertex: Vector2, secondVertex: Vector2):
    var vector: Vector2 = secondVertex - firstVertex
    var newWall = WALL_SCENE.instantiate()
    var collisionShape2D = newWall.get_child(0)
    var sprite2D = collisionShape2D.get_child(0)
    newWall.name = "Wall_" + str(wallCount)
    wallCount += 1
    collisionShape2D.shape.a = firstVertex
    collisionShape2D.shape.b = secondVertex
    sprite2D.position = (firstVertex + secondVertex) / 2
    sprite2D.rotation = vector.angle()
    sprite2D.scale.x = vector.length() / 6000
    add_child(newWall)
spark light
#

not sure if I understood you exactly but, why don't you create a signal that gets emitted in the player node before the instances get added to the scene and connecting the wall with this signal. you can disable the collision shape without setting the shape points to Vec2. Zero, by using the disable property, collisionshape2d.disabled = true

limber saffron
#

maybe you misunderstood what I meant. To explain better, what the script does is in the video. You can't see my cursor but it's actually me who click where I want the 2 twos vertices of each line to be. The problem is my character collide only with the last one created, and I don't know why

spark light
#

You want the player to collide with every wall?

limber saffron
#

yes

spark light
#

Can you activate in debugging options, show collision shapes?

#

Sorry, I'm pretty busy right now, I will think about it and text you later

limber saffron
limber saffron
#

ok I found the problem, but not the solution : the problem is all my walls have the same shape, but I don't know why. Logically(?) when I create a new wall with instantiate(), it also create a new collisionShape2D (because it's a subnode) right ?

#

it's even more strange because the sprite2D, which is a subnode of the collisionShape2D, works fine

#

Ok I found how to resolve the problem, I added collisionShape2D.shape = SegmentShape2D.new() before assigning values.

spark light
#

Nice, I think another approach would have been that you set the collision shape node to local to scene.

limber saffron