Asteroids like movement for the player in 3d top down.

as the title says.
I just cant seem to get it right.

as of right now I got it like this:

transform.Translate(Vector3.forward * Time.deltaTime * Speed);

if ((Input.GetKey(KeyCode.W)) && (Speed < MaxSpeed))
{
Speed = Speed + Acceleration * Time.deltaTime;
}
else if ((Input.GetKey(KeyCode.S)) && (Speed > -MaxSpeed))
{
Speed = Speed - Deceleration * Time.deltaTime;
}

(players looks at the mouse btw)
but the player keeps moving at the same speed even when turning around.
I want it so the player keeps moving in the same direction when it turns to keep looking at the mouse.

any help?

I think you need to determine the vector of the direction of travel and move the player in that direction instead of “Vector3.forward”

stain2319 has it right. You are always moving “forward” even if you rotate your ship around. What you need to do instead of have a separate vector that is your “movement” vector. Then when you thrust, you can still use the forward vector to add to that vector. Finally, you should use that vector’s size/distance as the speed as well so you can just add it to the transform each update. You can still put a speed limiter on it.

That said…when I was making this exact thing in the past, I used the physics system. You just add force in the direction you are thrusting(still using that forward vector as a basis in the case of thrusting forward). And you can still add a speed limit as well. Then it’s just about tweaking values for force, friction, etc… And if you wanted to add a feature that slows the ship down you just turn friction on and it happens.

1 Like

An asteroids game is simplified physics implementation.

Your ship has “velocity” which is its current speed. It is a vector. Meaning it is 2 dimensional. It never decreases on its own, meaning if you accelerate and stop engines, the ship will continue flying in a straight line (newton’s laws say hi)

The ship accelerates in direction pointed by ship’s nose and can rotate while moving.

That’s entire asteroids, pretty much.

1 Like

You just need to have the turning input code before the movement input code then the forward movement will always be towards the mouse.