Turret upgrade

This is a simple function that is called once the user has pressed 'upgrade' on the selected turret. All the turrets modifiers are increased as well as its next upgrade cost and sell price. 'Mathf.FloorToInt' is used to convert the float to an integer so that decimals don't get inlvolved with the simple integer currency in the game.


public void Upgrade()
{
	m_Level++;
	m_FireRate *= 1.25f;
	m_Damage *= 1.25f;
	m_Range *= 1.25f;
	float estimatedWorth = m_Worth * 2f;
	m_Worth = Mathf.FloorToInt(estimatedWorth);
	float estimatedUpgradeCost = m_UpgradeCost * 2.5f;
	m_UpgradeCost = Mathf.FloorToInt(estimatedUpgradeCost);
}

Turret Rotation

Using the targets position, the turrets position and quaternions, the turret can be rotated (around the attach point passed in) to look at the target. 'Quarternion.Lerp' sets the speed of the turret rotating each frame to make the rotating look more realistic.


Vector3 direction = m_Target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(direction);
Vector3 rotation = Quaternion.Lerp(m_RotationPoint.rotation, lookRotation, Time.deltaTime * m_RotationSpeed).eulerAngles;
m_RotationPoint.rotation = Quaternion.Euler(0f, rotation.y, rotation.z);

Target updating

Finds the nearest target by getting every game object in the scene with the tag 'Enemy', placing them all in a list. It then loops through the list measuring each distance between turret and enemy. Sets the enemy with the smallest distance to the target.


GameObject nearestEnemy = null;
GameObject[] activeEnemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in activeEnemies)
{
 if(!enemy.GetComponent().GetDying())
 {
  float distanceBetween = Vector3.Distance(transform.position, enemy.transform.position);
  if (distanceBetween < shortestDistance)
  {
   shortestDistance = distanceBetween;
   nearestEnemy = enemy;
  }
 }
}
				

Rocket splash damage

When a rocket reaches its target and explodes, using the 'Physcis.OverlapSphere' function it gets all game objects in that scene within the radius of the sphere and places it in a list. Then using some mathematics deals damage to the enemies near depedning on how far away they are - closer they are the more damage they take.


private void SplashDamage()
{
 Collider[] objectsWithinExplosion = Physics.OverlapSphere(transform.position, 7.5f);
 foreach (Collider nearbyObject in objectsWithinExplosion)
 {
  if(nearbyObject.gameObject.tag == "Enemy")
  {
   if(m_Target != nearbyObject.transform)
   {
    Vector3 middleOfEnemy = nearbyObject.transform.position;
    middleOfEnemy.y += 2.2f;
    Vector3 direction = middleOfEnemy - transform.position;
    float distanceToTarget = direction.magnitude;
    float distanceFromImpactToEnemy = distanceToTarget - 7.5f;
    distanceFromImpactToEnemy = -distanceFromImpactToEnemy;
    float damageMultiplier = distanceFromImpactToEnemy / 7.5f;
    float damage = this.GetComponentInParent().GetDamage() * damageMultiplier;
    nearbyObject.GetComponent().TakeDamage(damage, ProjectileType.Rocket);
   }
  }
 }
}

Feedback

TBA