(GDScript) Trying to either teleport the ball back into the middle of the screen or queue_free() the current ball and create a new instance of it
Asked Answered
T

1

1

The game is a head soccer game where the player has to try and score on the opponent. The enemy AI works by making the Vector position of the ball a global variable so that the enemy always has access to the ball's position in order to chase it. The problem I have ran into is that when creating a new ball instance, it creates an entirely new node which does not feed its position into the global variable that the enemy reads from, meaning the enemy will not follow the new ball.

The other option I have is to teleport the original ball back into the middle of the screen, but because it's a RigidBody2D, the teleporting functions don't work the same. I'm having trouble trying to figure out how to just put the ball in the center of the screen.

Essentially, when the ball enters the goal, I want the RigidBody2D Soccer Ball to teleport to the middle of the screen. If someone is capable of doing so, or knows anything that could help me towards my goal, that would be fantastic. I have been stuck on this problem in my coding class for a week now, and have no idea how to work around it.

Thanks!

Towrope answered 31/3, 2023 at 20:1 Comment(0)
P
2

The game is a head soccer game where the player has to try and score on the opponent. The enemy AI works by making the Vector position of the ball a global variable so that the enemy always has access to the ball's position in order to chase it. The problem I have ran into is that when creating a new ball instance, it creates an entirely new node which does not feed its position into the global variable that the enemy reads from, meaning the enemy will not follow the new ball.

The only way I make sense of this is that you are using a signal that is not connected for the new Node. If that is the case, you could connect the signals from code.

However, I would suggest to use an autoload to store the position, and then everybody should be able to read it or write it.


The other option I have is to teleport the original ball back into the middle of the screen, but because it's a RigidBody2D, the teleporting functions don't work the same. I'm having trouble trying to figure out how to just put the ball in the center of the screen.

I have covered the elusive RigidBody2D teleport in another answer. The way I suggest to do it is like this for Godot 3.x:

Physics2DServer.body_set_state(
    get_rid(),
    Physics2DServer.BODY_STATE_TRANSFORM,
    Transform2D.IDENTITY.translated(Vector2(512, 300))
)

The alternative is to use _integrate_forces, which you can find following the link.

Which isn't too hard to get working in Godot 4:

PhysicsServer2D.body_set_state(
    get_rid(),
    PhysicsServer2D.BODY_STATE_TRANSFORM,
    Transform2D.IDENTITY.translated(Vector2(512, 300))
)
Protolanguage answered 31/3, 2023 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.