Linear Physics Movement
So far we have learned about basics physics concepts and how Unity implements them. Today's tutorial will bring some of those concepts together to learn how to move objects linearly. We will demonstrated this concept by use of an example. In this tutorial, we are only going to use Physics to move the player. There may be other ways to move it them, but the goal for this tutorial is to accomplish both tasks with Physics. To start this tutorial add the top 4 variables above your start method and the two assignment statements in your start method. Also, make sure the script you created is attached to the object you will be moving in the screen.
Moving the Player
The first part that we will look at is how to move the player linearly. The traditional method that is used for player movement is to utilize transfrom.Translate, or some non-physics method of movement. Generally for player movement, we can use this method because it creates a linear movement in a direction that we would like to travel. For this example, let's assume that we would like our player to move using physics. We can achieve this by combining previous elements that we have learned about. One solution is to use Rigidbody.AddForce to add a force to our player to move them. Recall that when we are adding a force to the player, we are moving the player at some acceleration forward. While we do that, we have drag, friction, and gravity which are slowing the player down and bringing them to a stop. The net movement will be some immediate acceleration forward followed by slowing down and stopping. This creates a spike in initial movement followed by a decay. For player movement forward and backward, we can see that this is probably not the effect that we would like to achieve. What we want is to have uniform movement in one direction or another. This is why transform.Translate works so well for non-physics movement. However, it does not take into account drag, friction, or gravity. If our player was crossing a sticky material which might slow them down, they would not see a difference with transform.Translate. With Physics movement however, they would see a difference because it takes into account those values. How can we create this movement? Well we can still use Rigidbody.AddForce, we just need to add some additional logic. We need our player to stop immediately once we let go of a movement key. Let's add additional if statements for Input.GetKeyUp("s") and Input.GetKeyUp("d") and alter the velocity to make the player immediately stop once those keys are let go. Not when we press the movement keys down we move with Physics and are affected by things like gravity and friction, but do not have a decaying movement once we let the key go.
For this tutorial we would also like our player to jump which has gravity working against it. A non-physics jump that is realistic would not be very complex to code because not only do you have to code the up and down movement, you also have to code the varying in speed as it does so. Like in our previous example, we can use Rigidbody.AddForce to add an instantaneous acceleration to our player. The velocity would start off high and in the up direction and decay as gravity affects it till it reaches zero velocity. Then it will incur a velocity downwards increasing the players speed until it reaches a terminal velocity. This actually makes a pretty realistic jumping movement. If you don't believe me, try jumping and watching how you move in a mirror. You can also throw an object up in the air, and that is a similar effect.
For more tutorials, follow us on Twitter and Youtube!