Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Hogge · Aug 02, 2014 at 08:39 PM · destroytimerdistancenpc

Destroy happens immediately

I'm currently working on a Starfox-ish game. I've managed to get quite far today, as I've managed to make an enemy fly towards the player. What I want to happen is that once an enemy flies within a certain range of the player, it will fly over the player and then self destruct. This is a behaviour I've called "disengage".

At first I was uncertain how to make the enemy fly over the player (because midair collissions with every enemy would be stupid), then I created an actual prefab which is a child to the players plane, which the enemies try to fly in to.

However, once I wrote the code, I wasn't certain, but I thought it was targeting the "disengage target" straight away. Once I added the self destrct, however, I realised that the enemy plane self destructs immediately. It seems to bypass the timer, as it gets destroyed on frame 1, even if I set the timer to ludicrously large numbers, and the disengage distance to ludicrously small distances.

Anyways, here's the code:

 using UnityEngine;
 using System.Collections;
 
 
 public class EnemyFighterScript : MonoBehaviour {
     public float interceptvelocity = 10.0f ;
     public Transform Player;
     public float distance;
     public Transform target;
     public Transform disengagetarget;
     public float disengagedistance;
     public bool disengage;
     public float timer;
     // Use this for initialization
     void Start () {
         disengage = false;
     }
     
     // Update is called once per frame
     void Update () {
         distance =Vector3.Distance(transform.position,target.position);
         if (distance<disengagedistance){
             disengage=true;
         }
         if (disengage=false){
         transform.LookAt (Player);
         transform.position += transform.forward * interceptvelocity * Time.deltaTime;
         }
         if (disengage=true) {
             //makes enemy fly away and prepare self destruct after reaching proximity to player
         transform.LookAt (disengagetarget);
         transform.position += transform.forward * interceptvelocity * Time.deltaTime;
         timer =- Time.deltaTime;
 
         }
         if(timer <= 1){
             //destroys NPC once it passes player
             Destroy(gameObject);
         }
     }
 }
 

Comment
Add comment · Show 2
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 robertbu · Aug 02, 2014 at 08:54 PM 0
Share

Are you sure that 'timer' is getting assigned a value in the Inspector. If you did not, then the initial value will be 0.0. Note there is an optional second parameter for Destory() which is the time in the future you want the object to be destroyed. So you could use this form of Destory() and avoid the timer code.

avatar image Hogge · Aug 02, 2014 at 08:58 PM 0
Share

I did set the timer to 1000 in the inspector. These are my current settings: alt text

settings.png (33.7 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by AlucardJay · Aug 02, 2014 at 09:45 PM

not sure about your problem, but you have incorrect conditional operators at line 25 and line 29

 if (disengage=false)

should be

 if (disengage==false)

== is equal to

Comment
Add comment · Show 1 · 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 Hogge · Aug 03, 2014 at 01:29 AM 0
Share

That solved it! Thanks a lot!

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

hi guys, i need help 1 Answer

Hiding Texture2D after a sec 1 Answer

Destroy when out of range issue 0 Answers

Distance destroy object 3 Answers

Creating more objects the closer you get 0 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