Unity – Spaceship Shooting Game – JS – Laser Bolt

1. Create the Prefab ‘Bolt’
2. The prefabs needs inside Inspector:
– Transform
– Rigid Body
– Capsule Collider: check ‘Is Trigger’

– Mover.js

#pragma strict

var speed : float; // Assign this inside Inspector

function Start () : void {
    // forward is the Z Axis
    rigidbody.velocity = transform.forward * speed;
}

Inspector> Mover (Script)
– Speed: 20

3. Create a Prefab ‘Player’ (it is the spaceship)
4. Create an Empty Object ‘Shot Spawn’ (produttore di spari)
5. DRAG AND DROP ‘Shot Spawn’ over Hierarchy> ‘Player’, now ‘Shot Spawn’ is child of ‘Player’
6. DRAG AND DROP ‘Bolt Prefab’ over Hierarchy> ‘Shot Spawn’, now ‘Bolt Prefab’ is child of ‘Shot Spawn’

At the end the Hierarchy will be:

Player
– Shot Spawn
– Bolt Prefab

Assign to ‘Player’ PlayerController.js:

#pragma strict

class Boundary
{
    // Theese variables are public to make the code easy-reusable
    // You can setup theese variables from Inspector
    var xMin : float;
    var xMax : float;
    var zMin : float;
    var zMax : float;
}

var speed : float;
var tilt : float;
var boundary : Boundary;

var shot : GameObject;     // Inspector -> assign Bolt Prefab
var shotSpawn : Transform; // Inspector -> assign Shot Spawn Empty Object
var fireRate : float;      // Inspector -> 0.25 (seconds) = 4 shot per second

private var nextFire : float;

function Update () {
    // Get Input Fire button
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;
        // Return the clone of the GameObject, position, rotation if user click Fire button 
        Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
        // Assign an audio file inside Inspector
        audio.Play ();
    }
}

function FixedUpdate () {
     // Get User Input START
     var moveHorizontal : float= Input.GetAxis ("Horizontal");
     var moveVertical : float= Input.GetAxis ("Vertical");
     // Get User Input END

     // Move the GameObject
     var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement * speed;

    // Limitate movement inside the screen START
    rigidbody.position = new Vector3 
    (
        Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
        0.0f, 
        Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );
    // Limitate movement inside the screen END

    // Tilt movement START
    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    // Tilt movement END
}

Hierarchy> Player> Shot Spawn> Bolt CANC to delete
Se non si cancella questo oggetto, all’inizio del gioco la nostra astronave sparerà subito un colpo, invece noi vogliamo che non vengano emessi colpi fino a che il tasto fire resta inutilizzato.

7. MAIN tOP MENU> GameObject> Cube and name it ‘Boundary’
8. Hierarchy> select ‘Cube’> Inspector>
– Transform> small gear icon> Reset
– Mesh Render> uncheck
– Box Collider> check ‘Is Trigger’
9. Scale the box to surround all your game area

10. Create and assign to ‘Boundary’ DestroyByBoundary.js

#pragma strict

// Destroy Game Objects START
function OnTriggerExit(other : Collider)
{
    // Destroy all GameObjects WITH COLLIDER that run away THIS Collider
    // The GameObject WITHOUT COLLIDER will not be detroyed!!!
    Destroy(other.gameObject);
}
// Destroy Game Objects END

11. Hierarchy> ‘Boundary’> Inspector> Remove ‘Mesh Filter”Mesh Renderer’