Week 3: Basic Programming - Let's get the plane flying
Hey everybody, today I am going to show you the process of creation our paper plane for the Objective Uranus. Open Unity -> 2D Project. We will add a paperplane sprite and green sprite for background. Put them all in the scene
![](https://static.wixstatic.com/media/aa724d_dff8980053ee4076a0cc7e92f9873bbf.jpg/v1/fill/w_980,h_551,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/aa724d_dff8980053ee4076a0cc7e92f9873bbf.jpg)
Then we click add component -> Physics2D -> Polygon Collider 2D
![](https://static.wixstatic.com/media/aa724d_2ad82c91761f4411bafa89f589013fb9.jpg/v1/fill/w_980,h_551,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/aa724d_2ad82c91761f4411bafa89f589013fb9.jpg)
We do the same with the green background, but give box collider to it. Then create folder Scripts and add C# Script called Player. Add two public float variables called moveSpeed and resistanceVelocity. First one is for the speed of our main character and the resistanceVelocity we will use to push the plane down as he flies further .
![](https://static.wixstatic.com/media/aa724d_990ee8ae807845438b50f1ceb6ecadc9.png/v1/fill/w_980,h_528,al_c,q_90,usm_0.66_1.00_0.01,enc_avif,quality_auto/aa724d_990ee8ae807845438b50f1ceb6ecadc9.png)
So far we need only to write the update function. First we add the x axis movement. Since this is a paper plane it will always fly on the right direction. This is x, +1 direction.
![](https://static.wixstatic.com/media/aa724d_f4c8177afc7d4334babb1c9ffd3a06e7.png/v1/fill/w_980,h_528,al_c,q_90,usm_0.66_1.00_0.01,enc_avif,quality_auto/aa724d_f4c8177afc7d4334babb1c9ffd3a06e7.png)
So far, so good. Next thing we need to do is to add force that we will apply to the plane, so it falls down. For that we will need to add Rigidbody2D. Go back to the Unity scene, select the Plane, add component -> add Rigidbody2D. Remove the gravity and go back to Visual Studio.
![](https://static.wixstatic.com/media/aa724d_837bb9de5e794500a585b3b1002fd2b7.png/v1/fill/w_270,h_174,al_c,q_85,enc_avif,quality_auto/aa724d_837bb9de5e794500a585b3b1002fd2b7.png)
Use the AddForce function to apply the power on the RigidBody over the Y axis.
![](https://static.wixstatic.com/media/aa724d_6a381e8699c4408b8ef9094a708145d5.png/v1/fill/w_701,h_51,al_c,q_85,enc_avif,quality_auto/aa724d_6a381e8699c4408b8ef9094a708145d5.png)
Next thing is to check if the plane should move up or down. Tricky part is not to forget to add the right movement to the plane while the plane is moving on the Y axis. We are doing this with an If statement. This is how the up movement looks like.
![](https://static.wixstatic.com/media/aa724d_baffcb30b7564d30b06a5baaaabb82ec.png/v1/fill/w_888,h_170,al_c,q_85,enc_avif,quality_auto/aa724d_baffcb30b7564d30b06a5baaaabb82ec.png)
We do the same thing for the “Down” direction. The only difference is we use KeyCode.DownArrow and on the first transform che change the parameters to (0,-1,0). At the end just don't forget to add the script to the plane object.
And there it is. We have the Character for our Player.