ThrowBall.cs

With the GameBall selected, click on AddComponent | NewScript and create the ThrowBall.cs class. Grab this file from the downloads. We will not put the whole thing here, but we will go over the important area of the script:

Here, in our Update() function--remember that it's called every frame--ApplicationManager.Instance.ThrowBall is checked. If that bool is true, this code is executed:

  • throwDirection is a variable set by the current forward vector of the camera or your position. Simply put, the angle you are pointing to determines the angle the ball will be thrown in.
  • throwForce is a variable set by our BounceObjectToPoints class, based on the current location of the speed indicator light.
  • Using the rigid body, we apply the force throwDirection*throwForce.
  • As soon as we have sent the ball off, we set the Throwball bool back to false.

Now we need to add a rigid body so that the Unity physics engine can do its work.

With the GameBall still selected, click on Add Component | Physics | RigidBody. We can leave these setting at the default. We have everything done on our GameBall, and we need to turn it into a Prefab.

Click on the GameBall object, drag it down to the Prefabs folder in the Project view, and release it. Once you see the GameBall in the Prefabs directory, delete the GameBall object from the Hierarchy view.

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

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