11th February
2013
written by admin

This past week I spent a lot of time setting up the RPG gun, as well as working on a rough first pass on projectile class that allows me to track a target.  I think the RPG will help make the other guns much quicker to setup to work.  I also setup a the damage on the machine gun correctly now, which acts more as a “drain” instead of firing tons of projectiles.  My son ended up getting really interested in helping me tweak the values in the game which led to some really cool tests.  I’ve got some flying enemies that take damage and blow up correctly now.

Lets start with looking at the variables I need for the projectile.  ( Please note all this is still very early and not final at all )

  public GameObject currentTarget;
  public GameObject launcher;
  public float speed = 10.0f;
  public float damage = 10.0f;
  public float lifeTime = 5.0f;
  public float ignoreCollisionTime = 1.0f;
  public GameObject explosionParticle;
  public bool bRadius = false;
  public float Radius = 100.0f;
  public bool bFindNewTarget = true;
  private float aliveTime = 0.0f;
									

Most of these variables are self explanatory, but theres a few I want to go over.

  • launcher: This is the gun that fired the missile/projectile
  • ignoreCollisionTime: This is the time where the projectile ignores any collision, can usually be set to 0, but sometimes its necessary just to have this tweak-able option in there.
  • lifeTime: This is the time the missile will live if no collision is made.
  • bRadius: If true the missile does radius damage when it explodes

The targeting is really simple right now, in the future I want to add a few things to improve the projectile such as dampening rotation, and acceleration.  This will come in phase 2 of my work.

    if( currentTarget != null )
    {
      transform.LookAt( currentTarget.transform );
      transform.Translate( Vector3.forward * speed * Time.deltaTime );
    }
									

I also do work in the projectile for searching for a new enemy if a target is not found, it basically does a quick search for the nearest enemy and picks that as the new target.  This is only uses if the first target is destroyed, which in the future maybe handled a bit better by not allowing “overkill” to occur.  “Overkill” is what happens when something is targeted even though lethal damage will already be dealt.  If you’ve played Starcraft 1 you’ll understand the overkill concept because the Siege tanks would all fire at the same unity which wastes damage that could of been dealt to the same unit, where effectively 2 shots would of killed the unit.

Now onto collision

  void OnCollisionEnter( Collision other ) {
    
    if( aliveTime < ignoreCollisionTime )
      return;
    
    if( other.gameObject == launcher )
      return;
    
    if( other.gameObject.tag == "Enemy" )
    {
      EnemyMovement en = other.gameObject.GetComponentInChildren<EnemyMovement>( );
      if( en != null )
      {
        en.TakeDamage( damage );
      }
      Explode( );
    }
    else if( other.gameObject.tag == "Turret" )
    {
      Explode( );
    }
    else if( other.gameObject.tag == "Core" )
    {
      Core coreComp = other.gameObject.GetComponent<Core>( );
      coreComp.TakeDamage( damage );
      Explode( );
    }
  }
									

I’m really not sure if this is the best way to handle collision, but it seems effective enough.  I’m calling TakeDamage on the component when it is hit, in fact I may switch this over to “SendMessage” call in the future, but for now a public TakeDamage function is fine.  I also think I want to make a “HealthBase” that all damageable objects inherit from that way the damage interface is exactly the same for all objects.  That will probably be something I work on this week.

Explode is a pretty simple function that just detonates the projectile, spawns an explosion particle, and does radius damage.

  void Explode( ) {
    Instantiate( explosionParticle, transform.position, transform.rotation );
    
    if( bRadius )
    {
      GameObject[] enemies = GameObject.FindGameObjectsWithTag( "Enemy" );
      foreach ( GameObject en in enemies )
      {
        Vector3 toVec = en.transform.position - transform.position;
        if( toVec.magnitude < Radius )
        {
          EnemyMovement enComp = en.GetComponentInChildren<EnemyMovement>( );
          enComp.TakeDamage( damage );
        }
      }
    }
    Destroy( gameObject );
  }
									

This is the brunt of the work that I got done this last week in my free time.  I’m really happy with how this game is shaping up, and its really encouraging to me that my son is enjoying helping me with it even though he’s only 7.

Until Next time, thanks for reading.

Comments are closed.

RECENT POSTS

31st December 2014
03rd August 2014
25th June 2014