I have a player and an enemy. My player also has a child node called "ObjectContainer" which is basically a small collision box that moves from beneath the player to the top of the player (think of Mario picking up an item in Super Mario 2/Super Mario USA).

What I'm trying to figure out is what line of code would make my enemy node follow or "chase" the "ObjectContainer" node and stay hovering over that position unless specified otherwise.

Basically, make a node gradually and consistently move towards another node, wherever it's position may be.

I was thinking of trying add_child(), but I assume that would make the enemy node just suddenly pop into the position of the node it's now a child of. I also tried "body.velocity = body.position.direction_to($EnemyContainer.position) * body.speed", but the enemy just starts slowly rising vertically into the air forever for some reason.

Any ideas?

  • xyz and kkkkkkabc replied to this.
    • [deleted]

    • Best Answerset by NJL64

    NJL64 You need to assign position on every frame so that it's updated continuously. It should be done in _process() or _physics_process(), as those functions are automatically executed every frame. Currently you execute it only once when body enters the area.

    NJL64 Use global_position, not position.

      xyz I tried. I didn't get any different results. The enemy node continues to rise slowly into the air when entering the Area2D body even if I type global_position.

      • xyz replied to this.

        NJL64 I tried. I didn't get any different results. The enemy node continues to rise slowly into the air when entering the Area2D body even if I type global_position.

        body.global_position = $EnemyContainer.global_position

        Does that put the enemy at the position of the container?

          xyz Unfortunately, no. I tried that even before I opened up this discussion. I tried it just now to be double sure, and still same result. Enemy rises vertically upward to no limit. I even tried swapping which ones specify global_position.

          • xyz replied to this.

            NJL64 Well you need to set velocity to zero.

              xyz How should I go about doing it if body.velocity is already assigned a value? I'm not sure what you mean.

                NJL64

                body.velocity = Vector3(0,0,0)

                  NJL64 How should I go about doing it if body.velocity is already assigned a value? I'm not sure what you mean.

                  Who assigned it?

                    xyz I did. I changed it from "body.velocity = body.position.direction_to($EnemyContainer.global_position) * body.speed" to "body.velocity = 0", but that didn't do anything.

                    kkkkkkabc The collision number? Who's? What number should I change it to? Do you mean the mask/layers?

                      kuligs2 That causes an error. I'm not sure how it fixes the problem I'm trying to solve either. Also it's a 2D project. You can't use Vector3, right?

                      I'm not sure how setting velocity to 0 fixes the issue or how to implement that, where to put it, or who's velocity. Sorry, I'm just kind of confused.

                      • xyz replied to this.

                        NJL64 I'm not sure how setting velocity to 0 fixes the issue or how to implement that, where to put it, or who's velocity. Sorry, I'm just kind of confused.

                        Set body.velocity to zero. Since body.velocity is a vector, you need to set it to zero vector value as @kuligs2 suggested, not to scalar 0 value. If you're in 2D it needs to be Vector2(0, 0) or Vector2.ZERO. Once you have that sorted out, assign the container's global_position to body.global_position. Eliminate all other code that messes with body's velocity or position (global or local).

                        Doing the above should result in enemy being glued to the position of the container. This is a simplified version of what you ultimately want to achieve but in coding we often first implement a simpler version of the solution (sorta like a rough sketch), make sure that it works properly, and then proceed to gradually upgrade it into more finessed versions.

                        So first do the simple gluing, make sure that it works and that you understand how and why it works.

                          xyz Okay. Thanks for clarifying! I'm gonna take your advice and see what I can do.

                          This makes the body stop and remain still where it is when making contact with the Area2D:

                          This, makes the body fall through the floor:

                          Maybe, I'm misunderstanding.

                          *I can see I posted the wrong screenshot for the first pic. Sorry.

                          • xyz replied to this.

                            NJL64 Yes,collision layer/mask is what caused to rise.The collision number of different objects can be set to different.

                              NJL64 Maybe, I'm misunderstanding.

                              Your question was "How do you make one node follow the position of a node from another scene?". What this has to do with areas?