Step 4: Firing A Projectile

New Event Map

First, in your onSceneStart function you’ll want to push a new EventMap to capture the user clicking the left mouse button.
Have this event call the fire function which we will create next.

Fire Function

The new fire function will do 3 things, create the projectile, apply a force to throw it and delete it after 5 seconds.
First create the projectile by calling SceneManager.createObject.
Then set the objects position to the position of the Cameras eye.
Next set the objects scale to 0.3 (or however big you want your projectiles to be)

Apply Force

To make the projectile fly forwards, you can call applyForce on it and pass that function a vector.

The Mouse.getRay function will return a vector that is pointing in the direction of the mouse, so all you have to do is multiply that vector by the amount of force you want and use that in the applyForce function.
We’ll also set the color of the projectile to a random color.
The setColor function takes a color which is defined by 3 or 4 numbers between 0 and 1. RGBA

( Wild Pockets Manual : More details about forces. )


In order to make the projectile disappear after 5 seconds we can schedule  a function to be run later with Timer.later.
pass the function that you want to call, in this case calling delete on
the projectile, and the number of seconds later to call it.
Now you should be able to test your pocket and shoot colored balls into the scene.

( Wild Pockets Manual : More details about the timer and scheduling functions. )


    -- onSceneStart is called first to start the game
function onSceneStart()
-- Output to the debug console
print ("Hello World")
        -- Add camera controls
        -- Set initial camera position
        -- Add an event map to handle firing projectiles
    map =
        -- Create six targets
    for i=1,6 do
    -- Launch a projectile
function fire()
        -- Create the object
    local projectile = SceneManager.createObject("ecanaan/lowpoly_sphere")
        -- Position the object on the camera
        -- Apply a force in the direction of the mouse
    projectile:applyForce(Mouse.getRay() * 50000)
        -- Apply a random color to the new projectile
        -- Schedule this projectile to be removed after 5 seconds
    Timer.later( function() delete(projectile) end , 5)