Physics for our game

In this section we will apply some concepts of this chapter into our game. In particular, we will see how to detect when a sprinkle hits a panda and applies damage to it using the Physics engine.

Set up Pandas as a rigidbodies

Since we will take advantage of the Physics engine, we need to proper set up the Panda as that to be a physical object in the scene. This means giving it a rigidbody component.

Therefore, we can start by adding a Rigidbody2D component to the Panda prefab, and set its Body Type as Kinematic, as shown in the following screenshot:

Set up Pandas as a rigidbodies

In theory, we should have done, since the Panda is considered a physical object now. However, in the previous chapter, we have written a function that allows the Panda to move by assigning its new position directly on its Transform. Since now the Panda has a Rigidbody2D component, we cannot do in this way anymore (as we already explained previously in the Rigidbodies section). Therefore, we need to slightly modify the PandaScript. In particular, we need to get a reference to the rigidbody of the Panda, then use the MovePosition() function for Kinematic rigidbodies. Basically, we are applying what we have learnt in the Dealing with rigidbodies section.

Hence, open the script, and add the following private variable:

//Private variable to store the rigidbody2D
private Rigidbody2D rb2D;

Then, in the Start() function add this line at the end of the function:

//Get the reference to the Rigidbody2d
rb2D = GetComponent<Rigidbody2D>();

In the MoveTowards() function, we need to use the MovePosition() function on the Panda's rigidbody to change its position. In addition, we shouldn't use deltaTime anymore, and substitute it with fixedDeltaTime. As such, here is highlighted what is changed from the previous chapter:

//Function that based on the speed of the Panda makes it moving towards the destination point, specified as Vector3
private void MoveTowards(Vector3 destination) {
  //Create a step and then move in towards destination of one step
  float step = speed * Time.fixedDeltaTime;
  rb2D.MovePosition(Vector3.MoveTowards(transform.position,
    destination, step));
}

Note

We need to remember that the MoveTowards() function, now, should be called within a FixedUpdate() and not in the Update(). We will see this in Chapter 7, Trading Cupcakes and the Ultimate Battle for the Cake – Gameplay Programming. But you can have an example in the next section.

Finally, we can save the script, and we have done with the Panda.

Set up projectiles as rigidbodies

Similar to what we did for the Pandas, we need to add a Rigidbody2D component to all the projectiles, and again set the Body Type to Kinematic.

If you remember, also projectiles used to move by changing directly their Transforms, and we need to fix this, since they have a Rigidbody2D component as well.

Open the script, and as we did for the Pandas, add the following private variable:

//Private variable to store the rigidbody2D
private Rigidbody2D rb2D;

Then, in the Start() function, let’s get the reference to it:

//Get the reference to the Rigidbody2d
rb2D = GetComponent<Rigidbody2D>();

Now, we need to replace the Update() function with the FixedUpdate() one, since we are dealing with the Physics engine. Moreover, we need to slightly change the code (note also the use of the fixedDeltaTime):

// Update the position of the projectile according to time and speed
void FixedUpdate() {
  rb2D.MovePosition(transform.position + direction *
    Time.fixedDeltaTime * speed);
}

Note

A careful reader would notice that we are applying the equation of motion explained in the information box within the Dealing with rigidbodies section. In particular, here we have split the velocity into a direction and a speed (which multiplied them together give back the velocity).

Save the script, and let’s see how we can detect when a sprinkle hits a panda in the next section.

Detect sprinkles

In order to detect a collision between a sprinkle and a Panda, we need to add them both a Collider2D. You can choose the one that better suits your needs, I'll go for the Box2D collider. Then, you need to make one of them act as a Trigger, in our example we can take the Panda.

The next step is to implement the OnTriggerEnter2D() function, which is called from the Physics engine of Unity, on the PandaScript. As a result, we are able to detect when something hits the panda, and check if it is an actual sprinkle, so to apply damage to the panda with the Hit() function written in the previous chapter.

//Function that detects projectiles
void OnTriggerEnter2D(Collider2D other) {
  //Check if the other object is a projectile
  if(other.tag == "Projectile") {
    //Apply damage to this panda with the Hit function
    Hit(other.GetComponent<ProjectileScript>().damage);
  }
}

Note

Of course, we need to be sure that every object with the Projectile tag have a ProjectileScript component attached. This check is left as an exercise.

Finally, save the script, and we have done with Physics for our game, at least in this chapter. In fact, in the next chapter, we will use again the Physics engine, but for other reasons.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.214.230