• 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 /
avatar image
0
Question by Amarde01 · Oct 31, 2018 at 11:50 PM · instantiatedistancedetect

How to detect distance of a Game Object from an instantiated Game Object once

I am relatively new to Unity and right now I am having a problem with detecting distances between objects. I'm trying to make an explosion radius that will damage the player. I have the missile drop from the sky and when it hits the ground, it instantiates an explosion particle effect. So far, I've managed to figure out the distance between a game object and an instantiated one, but it repeatedly detects it so it instantly kills the player. I wan't to detect the distance of my player to the explosion once, then, if another explosion happens, it will detect it again. Here is the code for the detection:

         playerHit = GameObject.Find("Ground").GetComponent<RocketExplode>().playerHit;
         missile = GameObject.Find("Ground").GetComponent<RocketExplode>().explosionMissileCopy1;
         distance = Vector3.Distance(this.transform.position, missile.transform.position);
         if (distance <= 3 && playerHit == false)
         {
             healthloseamt = 50;
             rb.AddExplosionForce(300, gameObject.transform.position, 2);
             isHit = true;
             playerHit = true;
         }

code for instantiating the explosion:

         if (other.gameObject.CompareTag("MissilePlay1"))
         {
             Destroy(other.gameObject);
             Instantiate(explosionMissile1, other.transform.position, explosionMissile.transform.rotation);
             explosionMissileCopy1= Instantiate(explosionMissile1, other.transform.position, explosionMissile.transform.rotation);
             playerHit = false;
         }

Thanks in advance for your help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jordash · Nov 01, 2018 at 01:52 AM

You'll need to let us know the class and methods your code is running under otherwise it will be difficult to identify what exactly is wrong.


From a glance it looks like your first section of code is in the player object? I'm not sure what is calling this code, but if it is in update that would be why it is repeating more than once. Ideally the distance check would be called by the missile object, you can call it once on initialisation and it would be the logical place to find code related to the missile.


I'm also not sure what is going on with the tag check and double instantiation in the second section of code, or what is calling that. I have a suspicion it is in an update method as well, which it shouldn't be.


I'd recommend you look into Singletons for your ground and player objects, GameObject.Find isn't very performant and a singleton allows you to call and object that there is only ever one of from anywhere in your code with a simple reference.

Comment
Add comment · 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
0

Answer by Amarde01 · Nov 01, 2018 at 10:58 PM

Thank you very much for your help @Jordash! I managed to figure out how to get it to work by making the particle damage the player when instantiated, and then destroy itself.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

117 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 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

Help needed on getting distance value of chain of links after instantiation 0 Answers

Why is the method being called every frame even though it is InvokeRepeating? 1 Answer

Detect if player is in range? 2 Answers

Instantiate gameobject a certain distance away from transform 2 Answers

Checking the distance between an instantiated object and an existing object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges