Game Development Reference
In-Depth Information
Listing 4.15 Rotating a Game Object Around Its Axes with
the Arrow Keys
function Update ()
{
if (Input.GetKey ("up")) //if up key is pressed
{
//rotate upward around the side axis
this.transform.RotateAround (this.transform.
position,
this.transform.right, 20 * Time.deltaTime);
}
if (Input.GetKey ("down"))
{
//rotate downward around the side axis
this.transform.RotateAround (this.transform.
position,
this.transform.right, -20 * Time.deltaTime);
}
if (Input.GetKey ("right"))
{
this.transform.RotateAround (this.transform.
position,
this.transform.forward, -20 * Time.deltaTime);
}
if (Input.GetKey ("left"))
{
this.transform.RotateAround (this.transform.
position,
this.transform.forward, 20 * Time.deltaTime);
}
}
when the arrow keys are pressed. The rotation occurs around the
object's position and axes at a rate proportional to Time.deltaTime ,
which is the time passed since the last frame update. This creates
a nice smooth rotational movement.
Step 3. Save and attach the script to the cannon2 object in the
Hierarchy.
Step 4. Play. Use the arrow keys to rotate the cannon.
Step 5. Next we want the cannon to shoot when the mouse is clicked.
Create a new prefab from a sphere and add a rigidbody to it. Name
this prefab cannonball.
Step 6. Change aim.js as shown in Listing 4.16 to add shooting
capability.
Search Nedrilad ::




Custom Search