0 votes

I've been experimenting with 2D portals in my game. I have a simple script that queue_frees a ball once it enters one portal, and spawns one in in the other.

I want to preserve the velocity of the ball, so that it doesn't just have zero velocity when it comes out of the portal. For this, I used the linearvelocity of the preexisting ball before it gets queuefreed, and apply that to the new ball once it comes out of the portal.

However, this does not align with the rotation of the portal, and the ball always continues to go in whatever direction it was going in. I want to be able to have the same velocity and momentum, but come out in the direction the portal's facing.

Does anyone know a variable I could use to modify this? How can I make the 2D portals realistic?

in Engine by (43 points)

When the velocity is a Vector2 just use Vector2.rotated().
velocity = velocity.rotated(portal.rotation)

Thank you, that works perfectly!

I will add it as answer. So that the question is not showen in the unanswered tab.

1 Answer

+1 vote
Best answer

Use the Vector2.rotated() methode like this:
velocity = velocity.rotated(portal.rotation)

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