0 votes

Hello
I'm new at godot engine. I'm trying to make a space shooter game and I've encountered a little problem.
I am moving my ship with that code:

if button_up.is_pressed():`
    velocity += Vector2(acceleration, 0).rotated(rotation + deg2rad(-90))
move_and_slide(velocity)

I made it so ship moves to where it's pointing and it works pretty fine. However when I'm moving to degrees between 1 and 89, 91 and 179 etc., both velocity.x and velocity.y keep rising until it reaches maxspeed and that results in my ship moving directly 45, 135, 225 or 315 degrees and not where it's pointing.
I want to make it so when for example velocity.x reaches max
speed, vector.y gets limited to its current value. Is it possible?

Thank you in advance and excuse me for my english

in Engine by (12 points)

1 Answer

0 votes

A crude way I can think to do it is just by some checks. Something like

var currentY = velocity.y
var currentX = velocity.x

# The code you currently have would go here

if velocity.x == maxspeed:
    velocity.y = currentY

# You would have the same logic again, just swap x and y

move_and_slide(velocity)

This may not work exactly if copy/pasted, but this general idea is what you could try. Store the old velocity, run your current code which is updating velocity, compare the new velocity with maxspeed, and if either is the same or greater than maxspeed you can reset the values to the old velocity.

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