Making our Ship Functional
So, let’s make the ship move around! There are two things we need to make this happen.
To move, the ship requires two things.
- It needs what is called the RigidBody2D
- Script called Move With Arrows.
RigidBody2D
RigidBody2D is vital to any object because it applies physics properties like gravity and mass.
- In the Hierarchy window, select the ship.
- In the Inspector, click Add Component.
- In the dropdown, look for Physics 2D and click RigidBody2D
- Once the RigidyBody2D component is added, run the program. What happens?
- We don’t want it to fall through the ground, so let’s change the Gravity Scale to zero.
- We also don’t want to make the ship fly too fast; we will add some Linear Drag, which is also like friction. Change the Linear Drag to 10.
What about making it move around? That’s where we add code. Lucky for us, we get to use a pre-built script.
Move With Arrows
- Click on the Add Component again
- This time, type in the Search bar Move With Arrows
- After you add the script, press play. If you use the arrows, the ship should move around.
- Now, change the speed variable in the Inspector and see what happens.
What about obstacles and collisions?