I need a coder C#
Budget: $10 – $30 USD
Step 3: Drive the ship
For this step, you're applying thrust to drive the ship forward.
1. Select Edit > Project Settings > Input from the top menu bar. Expand Axes (if necessary)
and add 1 to Size. Rename the bottom axis Thrust, set it to use space for the Positive
Button, and delete the entry for the Alt Positive Button.
2. Attach a Rigidbody 2D component to the Ship game object.
3. Create a new Scripts folder and add a new C# script named Ship to that folder.
4. Double click the Ship script to open it in Visual Studio.
5. Add a documentation comment above the line declaring the class.
6. We don't want to have to retrieve the Rigidbody 2D component every time we apply
thrust, so declare a field to hold that component and add code to the Start method to set
that field to the Rigidbody 2D component attached to the ship. Note: Declaring a field is
just like declaring a variable. Declare the field below the line that starts public class
Ship.
7. Add a Vector2 field called thrustDirection and set the field to a new Vector2 with an
x component of 1 and a y component of 0. We're doing this so we don't have to create a
new Vector2 every time we apply thrust to the ship.
8. Declare a constant in the class called ThrustForce and give the constant a reasonable
value (you'll probably have to experiment with this to get a reasonable value once you
can drive the ship!).
9. For physics-based actions (like applying thrust), we should use the MonoBehaviour
FixedUpdate method. Add a FixedUpdate method with the appropriate return type and
parameter list to your Ship script (the easiest way to do this is by just copying the
example from the Unity Scripting Reference documentation for the method and deleting
the code between the { and the }). Add a documentation comment above the new method.
10. Add code to the FixedUpdate method to detect input on the Thrust axis and apply the
ThrustForce in the thrustDirection to the rigidbody (remember, you saved that in a
field) if there's input on that axis. Use the Input GetAxis method to check for input on
the Thrust axis. Your first argument to your call to the AddForce method should be
ThrustForce * thrustDirection. You should NOT be applying an impulse force here,
so make sure you use the appropriate ForceMode2D value for the second argument.
11. Add the Ship script as a component to the Ship game object.
12. Use GitHub Desktop to commit your changes to your local and remote repos.
When you run your game, you should be able to drive the ship forward using the space bar. Of
course, your ship just drives off the right edge of the screen, never to return!
For this step, you're applying thrust to drive the ship forward.
1. Select Edit > Project Settings > Input from the top menu bar. Expand Axes (if necessary)
and add 1 to Size. Rename the bottom axis Thrust, set it to use space for the Positive
Button, and delete the entry for the Alt Positive Button.
2. Attach a Rigidbody 2D component to the Ship game object.
3. Create a new Scripts folder and add a new C# script named Ship to that folder.
4. Double click the Ship script to open it in Visual Studio.
5. Add a documentation comment above the line declaring the class.
6. We don't want to have to retrieve the Rigidbody 2D component every time we apply
thrust, so declare a field to hold that component and add code to the Start method to set
that field to the Rigidbody 2D component attached to the ship. Note: Declaring a field is
just like declaring a variable. Declare the field below the line that starts public class
Ship.
7. Add a Vector2 field called thrustDirection and set the field to a new Vector2 with an
x component of 1 and a y component of 0. We're doing this so we don't have to create a
new Vector2 every time we apply thrust to the ship.
8. Declare a constant in the class called ThrustForce and give the constant a reasonable
value (you'll probably have to experiment with this to get a reasonable value once you
can drive the ship!).
9. For physics-based actions (like applying thrust), we should use the MonoBehaviour
FixedUpdate method. Add a FixedUpdate method with the appropriate return type and
parameter list to your Ship script (the easiest way to do this is by just copying the
example from the Unity Scripting Reference documentation for the method and deleting
the code between the { and the }). Add a documentation comment above the new method.
10. Add code to the FixedUpdate method to detect input on the Thrust axis and apply the
ThrustForce in the thrustDirection to the rigidbody (remember, you saved that in a
field) if there's input on that axis. Use the Input GetAxis method to check for input on
the Thrust axis. Your first argument to your call to the AddForce method should be
ThrustForce * thrustDirection. You should NOT be applying an impulse force here,
so make sure you use the appropriate ForceMode2D value for the second argument.
11. Add the Ship script as a component to the Ship game object.
12. Use GitHub Desktop to commit your changes to your local and remote repos.
When you run your game, you should be able to drive the ship forward using the space bar. Of
course, your ship just drives off the right edge of the screen, never to return!