In this recipe, we show you how to create a custom script to control a game object based on physics. It is not the final controller for the character of our prototype: it's a small piece of code that can be used for a classic 2D shooter, based on physics.
In the Scripts folder in the Project panel, create a new C# script and name it PacktController
. Then, double-click on it to open it in Monodevelop.
First we need a bunch of variables to control the forces applied to the GameObject. Add the following lines at the beginning of the script, right below the usual line with the script class declaration:
(public class PacktController : MonoBehaviour {): public float horAcceleration; public float cruiseSpeed; //max speed when not pressing public float maxSpeed; //max speed while pressing public float actualSpeed; //speed at given time public float limY; //limit on y, use as mathf.abs public float expon; //used to smooth vert movement speed...