0 votes


I am trying to learn this engine by doing a simple 2D plateformer. Everything is fine except when I am trying to create ennemies enities.

I created a scene wich cointains the nodes needed for an ennemies:
- KinematikBody2D
-- CollisionShape
-- AnimationPlayer

Then I created a script to code the behavior of this entity.

Next I instanciated this scene in the main scene and everything is working fine.

The problem occur when I instanciated a second ennemy from the same scene but at a different position..
When I am interacting with one of them the other is affected too.
It's looking like they use the same variable of the script.

Here is the script for thoses entities

enum direction { LEFT, RIGHT }

const MAX_H_SPEED = 50

var velocity = Vector2(0, 0)
var oldVelocity = Vector2(0, 0)
var originPos = Vector2(0,0)

const maxPositionOffest = 100

signal collisionDetected()

func _ready():
    var Animation = get_node("AnimationPlayer")
    originPos = get_pos()
    direction = RIGHT

func _process(delta):
    velocity = oldVelocity
    var currentPos = get_pos()

    if (currentPos.x < originPos.x - maxPositionOffest ):
        direction = RIGHT
        velocity = Vector2(0,0)
    elif (currentPos.x > originPos.x + maxPositionOffest ):
        direction = LEFT
        velocity = Vector2(0,0)

    if(direction == RIGHT):
        velocity = Vector2( MAX_H_SPEED * delta, 0)
        velocity = Vector2( -MAX_H_SPEED * delta, 0)

    oldVelocity = velocity

    if(is_colliding()) :
in Engine by (12 points)

When I am interacting with one of them the other is affected too.
It's looking like they use the same variable of the script.

What do you mean? Are you talking about collisionDetected signal, or what?

Can you show the code where you create instances of your enemy scene?

you need to show the "interaction" too, what is connected to the "collisionDetected" signal (it does not seems to specify who emit it or some other data).

I didn't create the instance of the second ennemy by code, I just add the node to the main scene.

By interaction I mean when the player character collide with the left dude for example It will push it: normal since the 2 of them inherits for a Kinematicbody.
But he will also push the right dude wich is far away from him!

You can see the bug here:
From 0 to 16 sec eveything is working"as expected", from 16sec to en the end I reproduced the unexpected behavior

Please find my project on this git repositoy if you want to look into it:

Please log in or register to answer this question.

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.