This is the basic bullet that the tank will shoot. The behaviour is simple, when the missile hits one of the aliens the both disappear. If the missile don’t collide with any alien it will destroy itself in 2 seconds.
In Unity, once you have created the missile model (the texturization will be made later) you include in your project and add some Components. This components are:
One RigidBody that will not use the Gravity simulation, because I want that the missle moves straight.
One Collider to know if the missile collides with the rest of models in the game (particulary with aliens and barriers )
One Particle System that will simulate the “halo” or “smoke” that impulse the missile (is only an effect )
One usual Script that kill the object in a time, because if the player don’t hit any other model, we want that the Missile dissapear.
Finally, this model must have a Tag that Identify as a “Bullet” so when the collision appear, the model that receives the collision knows who are hitting it.
The code below shows the script “MissileBehaviour”. This scripts knows if any collision is produced, and if it was with a barrier or an alien, destroy the missile. However, if the missile don’t collide with anything, it will be destroyed when the life_time has passed.
using UnityEngine;
using System.Collections;
public class MissileBehaviour : MonoBehaviour {
// Use this for initialization
public float life_time = 2.0f;
private float amountOfTime = 0.0f;
void Start () {
}
void OnTriggerEnter(Collider other)
{
if( other.tag == "alien" || other.tag == "Barrier" )
{
Destroy(this.gameObject);
}
}
void Update()
{
amountOfTime += Time.deltaTime;
if( amountOfTime > life_time )
{
Transform[] ts = gameObject.GetComponentsInChildren<Transform>();
foreach( Transform child in ts )
Destroy( child.gameObject );
Destroy(gameObject);
}
}
public void changeLifeTime( float newLifeTime )
{
life_time = newLifeTime;
amountOfTime = 0.0f;
}
}