- Home /
Other
Do damage based on a certain object
Hi, so I am making a tower defense game. I am wondering how I can make it so the damagePerHit is different for every tower after I upgrade it, and the enemy knows what damage to take based off of what tower shot it. I have waypoints on every block that the enemy is moving on and that the towers can sit on. How would I do this? Thanks!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class EnemyTrigger : MonoBehaviour { [SerializeField] int hitPoints; [SerializeField] GameObject explosion; [SerializeField] EnemyMovement enemyMovement; [SerializeField] ParticleSystem hit; [SerializeField] GameObject atEndParticles; [SerializeField] float waitToDestroyAtEnd; [SerializeField] AudioClip hitSFX; [SerializeField] AudioClip explosionSFX; [SerializeField] AudioClip shootSFX; [SerializeField] Tower tower; TextController textController; PlayerHealth playerHealth;
public bool isDead = false;
public int damagePerHit;
bool atEnd = false;
void Awake()
{
textController = GameObject.Find("Text Controller").GetComponent<TextController>();
}
void OnParticleCollision(GameObject other)
{
Damage();
Explode();
}
void Damage()
{
GetComponent<AudioSource>().PlayOneShot(hitSFX, 5);
GetComponent<AudioSource>().PlayOneShot(shootSFX);
hit.Play();
hitPoints = hitPoints - damagePerHit;
}
void Explode()
{
if (hitPoints <= 0)
{
BoxCollider enemyBoxCollider = gameObject.GetComponent<BoxCollider>();
enemyBoxCollider.enabled = false;
isDead = true;
enemyMovement.isMoving = false;
explosion.SetActive(true);
GetComponent<AudioSource>().PlayOneShot(explosionSFX);
textController.points = textController.points + textController.pointIncrease;
textController.scoreText.text = textController.points.ToString();
Invoke("Destroy", 1f);
}
}
void Destroy()
{
Destroy(gameObject);
}
void OnTriggerStay()
{
atEnd = true;
Invoke("CheckIfDead", waitToDestroyAtEnd);
}
void CheckIfDead()
{
if (isDead == true)
{
return;
}
else if (isDead == false && atEnd == true)
{
GameObject spawnedParticles = Instantiate(atEndParticles, gameObject.transform.position, Quaternion.identity);
Destroy(spawnedParticles, waitToDestroyAtEnd);
playerHealth = GameObject.Find("Friendly_Base").GetComponent<PlayerHealth>();
playerHealth.HurtFriendlyBase();
Destroy();
}
}
}
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Waypoint : MonoBehaviour {
[SerializeField] Tower towerPrefab;
[SerializeField] Tower tower;
[SerializeField] Transform towers;
[SerializeField] PathFinder pathFinder;
[SerializeField] TextController textController;
public bool hasTower;
public bool towerIsLevel1;
public bool towerIsLevel2;
public bool towerIsLevel3;
[SerializeField] EnemyTrigger enemyTrigger;
// public ok here as it is a data class
public bool isExplored = false; // ok as is a data class
public Waypoint exploredFrom;
public bool isPlaceable = true;
public bool isPath;
Vector2Int gridPos;
const int gridSize = 20;
public int GetGridSize()
{
return gridSize;
}
public Vector2Int GetGridPos()
{
return new Vector2Int(
Mathf.RoundToInt(transform.position.z / gridSize),
Mathf.RoundToInt(transform.position.x / gridSize)
);
}
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
CheckForUpgrade();
if (pathFinder.numberOfTowers != pathFinder.towerLimit && isPlaceable == true)
{
SpawnTower();
var updatedTowerText = textController.towersLeft = textController.towersLeft - 1;
textController.towerText.text = updatedTowerText.ToString();
}
else if (pathFinder.numberOfTowers == pathFinder.towerLimit && hasTower == false)
{
print("Tower Limit Reached!");
}
}
}
public void SpawnTower()
{
float towerX = transform.position.x;
float towerZ = transform.position.z;
Vector3 towerPos = new Vector3(towerX, 20f, towerZ);
Tower spawnedTower = Instantiate(towerPrefab, towerPos, Quaternion.identity);
hasTower = true;
spawnedTower.transform.parent = towers;
isPlaceable = false;
pathFinder.numberOfTowers = pathFinder.numberOfTowers + 1;
towerIsLevel1 = true;
}
void CheckForUpgrade()
{
if (isPlaceable == false && textController.points >= tower.scoreNeededForLvl2 && isPath == false && textController.points < tower.scoreNeededForLvl3
&& towerIsLevel3 == false && towerIsLevel1 == true && towerIsLevel2 == false)
{
print("Upgraded To Level 2");
textController.points -= tower.scoreNeededForLvl2;
towerIsLevel1 = false;
towerIsLevel2 = true;
towerIsLevel3 = false;
}
else if (isPlaceable == false && textController.points >= tower.scoreNeededForLvl3 && isPath == false && textController.points > tower.scoreNeededForLvl2
&& towerIsLevel2 == true && towerIsLevel3 == false && towerIsLevel1 == false)
{
print("Upgraded To Level 3");
textController.points -= tower.scoreNeededForLvl3;
towerIsLevel2 = false;
towerIsLevel3 = true;
}
}
}
Answer by trebal · Aug 07, 2018 at 07:32 AM
Since the damage is determined by the variable damagePerHit, you can just set the damage done by each turret in the moment you upgrade them. That variable will be independent for each turret instance. For example:
if (isPlaceable == false && textController.points >= tower.scoreNeededForLvl2 && isPath == false && textController.points < tower.scoreNeededForLvl3
&& towerIsLevel3 == false && towerIsLevel1 == true && towerIsLevel2 == false)
{
print("Upgraded To Level 2");
textController.points -= tower.scoreNeededForLvl2;
towerIsLevel1 = false;
towerIsLevel2 = true;
towerIsLevel3 = false;
damagePerHit *= 1.5f;
}
That way, you would be incrementing the damage by each upgrade a 50 % each time of the current damage.
Also, why using booleans to determine the level of the turret ? You could use better and integer or even an enum to track them more consistently.