Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 07, 2018 at 08:13 PM by jacobawesome8 for the following reason:

Different problem

avatar image
0
Question by jacobawesome8 · Aug 07, 2018 at 04:57 PM · c#particlesdetectiontower defense

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();
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacobawesome8 · Aug 07, 2018 at 06:55 PM 0
Share

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?

avatar image madks13 jacobawesome8 · Aug 07, 2018 at 10:38 PM 0
Share

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

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges