Unity – Spaceship Shooting Game – JS – Asteroid
1. Hierarchy> Asteroid (Empty Object)
– prop_asteroid_01, (3d model of Asteroid), child of Asteroid
Random Rotate
2.Hierarchy select Asteroid and assign this scripts:
RandomRotator.js
#pragma strict // Ruzzolare var tumble : float; // setup this variable inside Inspector function Start () : void { // Random.insideUnitSphere function give a random Vector 3 value rigidbody.angularVelocity = Random.insideUnitSphere * tumble; }
NOTICE: Random.insideUnitSphere function give a random Vector 3 value
Destroy
DestroyByContact.js
#pragma strict // Destruction of asteroid START function OnTriggerEnter(other : Collider) { // If there is a GameObject with Tag Boundary ignore it if (other.tag == "Boundary") { // Return and does not reach - Destroy - commands lines return; } // Else it destroies other GameObject and Itself Destroy(other.gameObject); Destroy(gameObject); } // Destruction of asteroid END
Destroy Advanced
DestroyByContact.js
#pragma strict // Destruction of asteroid START var explosion : GameObject; // Assign GameObject inside Inspector var playerExplosion : GameObject; // Assign GameObject inside Inspector function OnTriggerEnter(other : Collider) { // If there is a GameObject with Tag Boundary ignore it if (other.tag == "Boundary") { // Return and does not reach - Destroy - commands lines return; } // Play explosion Instantiate(explosion, transform.position, transform.rotation); // If there is a GameObject with Tag Player Play playerExplosion if (other.tag == "Player") { Instantiate(playerExplosion, other.transform.position, other.transform.rotation); } // Destroy other GameObject and Itself Destroy(other.gameObject); Destroy(gameObject); } // Destruction of asteroid END
Move
Mover.js
#pragma strict var speed : float; // Assign this inside Inspector function Start () : void { // forward is the Z Axis rigidbody.velocity = transform.forward * speed; }
Prefab the asteroid
The Asteroid is ready to become a Prefab
1. Hierarchy> DRAG AND DROP ‘Asteroid’ inside Assets> ‘Prefab’ folder
2. Hierarchy> Asteroid> CANC to delete it