- Home /
Different problem
How to detect where particles came from
Hi, so I am making a tower defense game and I am making it so my particles shoot out of a tower, and when those particles hit the object, it does damage to it. I got that down but now I am trying to make it so you can upgrade the towers. I got it so the upgraded towers get placed down but the problem is the enemy only cares about hits from a tower with the script tower on it. So I made a script called towerlevel2 that does more damage than just the regular tower script. So how do I make it so that enemy knows how much damage to do based on which tower shot it. Thanks! Here is the script that does damage to the enemy:
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; TextController textController; PlayerHealth playerHealth; Tower tower;
public bool isDead = false;
bool atEnd = false;
void Awake()
{
tower = GameObject.Find("Tower").GetComponent<Tower>();
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 - tower.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();
}
}
}
Answer by madks13 · Aug 07, 2018 at 06:50 PM
I'd say, never self destruct objects. Have some other class handle the logic of it. And since you do that, you can also move the call to damage to the new class, which would know who initiated the damage.
Sorry but what do you want me to do? $$anonymous$$ake a new script that destroys the objects and bring the void damage to it?
Usually, we avoid self destoying objects. The reason is that you might have other objects that have a reference to it trying to access it, which would cause a crash.
That is why you usually have a class that manages the removal of dead objects. You could have a $$anonymous$$onoBehaviour class that does a LateUpdate and checks for any dead bjects.
As for the damage, in simple games objects interact directly. However, given that you use things like particles to do damage, i'd suggest an event system. Your particles might have a property that references the owner (the tower), and emit an event of dealing damage to anything it enters into contact. This way you can keep towers and units interacting directly with only a small script doing the cleaning.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
How to get raycast working on 2d objects 1 Answer
How to Pause ParticleSystem on Jump? 2 Answers