Now that the script is set up we can remove the Mesh
Renderer and Cube (Mesh Filter) components.
I decided to add some space debris to keep things
interesting as well as boost score, to do this add new game object and rename
it to Asteroid and set the transform Z-axis to 8. Taking an asteroid model from
the models folder drag it to the Asteroid parent object in the hierarchy making
the asteroid model a child. Select the Asteroid parent and add component
physics, rigidbody and deselect use gravity then add another component physics,
capsule collider then alter the collider to match the rough size of the
asteroid. With the asteroid game object selected add component, new script and
rename it to RandomRotator. Open this script and edit it
Public class RandomRotator : MonoBehaviour
{
Public float tumble;
Void start ()
{
GetComponent<rigidbody>().angualrVelocity =
Random.insideUnitSphere * tumble;
}
}
Select the Asteroid parent and set the tumble value to 5 and
change the angular drag to 0 so that it doesn’t slow down, add component new script
and name it DestroyByContact and open it
Public class DestroyByContact : MonoBehaviour
{
Void onTriggerEnter(Collider other) {
If (other.tag == ”Boundary”)
{
Return;
}
Destroy(other.gameObject);
Destory(gameObject);
}
}
Now select Boundary and where it says Untagged click it and
change it to Add tag which will bring up tags and layers in the inspector, in
element 0 type in Boundary, and now back in the Boundary inspector click again
on the Tag and change it from Untagged to Boundary.
Now to make the game more appealing visually I decided to
add some explosions, I started by opening up the DestroyByContact code
Public class DestroyByContact : MonoBehaviour
{
Public GameObject explosion;
Public GameObject playerExplosion;
Void onTriggerEnter(Collider other) {
If (other.tag == ”Boundary”)
{
Return;
}
Instantiate(explosion, transform.position,
transform.rotation);
If (other.tag == “Player”){
Instantiate(playerExplosion, other.transform.position,
other.transform.rotation);
}
Destroy(other.gameObject);
Destory(gameObject);
}
}
Now in unity in the prefab VFX folder I took explosion
asteroid and dragged it to the explosion slot to create a reference to it on
our destroy by contact script. Now selecting the player in the hierarchy change
the tag from Untagged to Player after that select the Asteroid parent and drag
an explosion effect to the player explosion slot in the inspector.
Now drag the Mover script onto the asteroid
parent inspector and set its speed to -5 so that the asteroid will travel down
towards the player. Now drag the asteroid parent onto the prefab folder so now
we can delete the asteroid parent from the hierarchy.
Comments
Post a Comment