How I Made Asteroids - Part 1
Introduction
First, we will tackle the hardest but most essential component of the game: Ship movement. The ship will rotate with the left and right arrow key, and move forward with the up arrow key. Here is something important that I failed to understand in the beginning: The ship doesn't move to another direction when we rotate it, it only... rotates... This seems obvious, but to me it was quite tricky to understand. So, if the ship is moving to direction x, and we press the left arrow key, the ship will still move to direction x. Only its head will point to a more left, or counterclockwise direction.
Ship Rotation
This requires some basic maths, with some knowledge how Unity works.
Rotate_Ship(string direction){
if(direction.Equals("right")){
ship_z_angle = -90*Time.deltaTime * 5;
}
else if(direction.Equals("left")){
ship_z_angle = 90 * Time.deltaTime *5 ;
}
ship_z_angle_snapshot += ship_z_angle;
ship_body.eulerAngles = new Vector3(0, 0, ship_z_angle_snapshot);
new_ship_x_position = (Mathf.Sin(ship_z_angle_snapshot * Mathf.Deg2Rad + Mathf.PI));
new_ship_y_position = (Mathf.Cos(ship_z_angle_snapshot * Mathf.Deg2Rad));
new_ship_position = new Vector3(new_ship_x_position, new_ship_y_position, 0f);
}
ship_z_angle
and ship_z_angle_snapshot
both store angle values in degrees. The former stores how many degrees the ship will rorate. ship_z_angle_snapshot
then adds or substracts from the value of ship_z_angle
, depending on whether we choose to rotate right (clockwise), or left (counterclockwise). We then have a functionality that stores how much the ship rotates and the new angle of the ship. But we have to remember that it is still in degrees.
We then rotate the ship for real and use the function eulerAngles
. This function will rotate the ship to the value of the degree we gained from the previous step, which is stored it ship_z_angle_snapshot
.
Lastly, we have to determine where the ship will move after it points to another direction. If we don't do this, the ship will move to the initial direction no matter how much we rotate it. That's because we haven't determined a new movement vector. And this is where some basic maths come into place.
Remember how we project a vector to the x and y coordinates. If we have a vector A and want to determine where, on the cartesian plane the endpoints are, we have to multiply its length by cos(a) where a is the angle between A and either the x or y line, depending where we begin to calculate the angles. In Unity's case, the y line is angle 0. We always normalize the Vectors we get, so the length of A is 1 (do you know why?), and project it to both x and y. Notice that we have to convert ship_z_angle_snapshot
to radians first because apparently Unity is so nice and uses both degrees and radians for different purposes. Last, we store the projections to a new vector, new_ship_position
.
Congrats, now you should be able to rotate the ship!
Ship Movement
Now we will make the plane move according to the direction it points. The code is quite simple.
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3.Normalize(new_ship_position);
accumulated_velocity += new_ship_position * velocity;
}
ship_body.position += accumulated_velocity * Time.deltaTime;
}
If we press the upper arrow key, we will first normalize new_ship_position
because we want it to always have the same length, no matter where the ship points. Normalizing makes our lives easier by turning the length of the corresponding vector to 1. That means we can easily control how fast our ship will go by controlling another variable, velocity
.
Last, let's make the ship move. We do it by adding the new vector to the previous position vector. Voila, the ship should be moving now. Don't forget to use Time.deltatime
to make the movement even smoother.
Asteroids
Pew pew pew!
Status | In development |
Author | mdlaras |
Genre | Action |
Tags | Asteroids |
More posts
- How I Made Asteroids - Part 0Mar 19, 2021
Leave a comment
Log in with itch.io to leave a comment.