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 /
avatar image
0
Question by Supermacka · Oct 18, 2018 at 08:14 PM · boolontriggerenter2dcooldown

How to create a cooldown for my homingMissiles?

Hey there,

I have two players(Player 1 & PLayer 2, it is a local game) and a powerup which spawns a homingmissile (prefabs) from the corresponding player that moves over the powerup. This missile then follows the other player and tries to kill them (before its 10second lifetime is over). At the same time I want the missile to be able to kill the player that sets it in motion, but if I do so the missile spawns and kills the player instantly when they move over the powerup.

That is why I have tried to create a cool-down timer before the missiles can destroy any players. My recent try was to set a boolean to (true) when the coolDownTimer is 0 for a homingmissile and only then it is supposed to be able to kill a player, but it does not work.

Script for reference:

[This script is for the powerup it self which spawns the homingmissiles]

 public class HomingMissile_PU : MonoBehaviour
 {
 
     public SpriteRenderer homingMissile1;
     public SpriteRenderer homingMissile2;
 
     public GameObject homingMissileObject1; 
     public GameObject homingMissileObject2;
 
     HomingMissile1 homingMissileScript1;
     HomingMissile2 homingMissileScript2;
 
     private void Start()
     {
         homingMissileScript1 = homingMissileObject1.GetComponent<HomingMissile1>();
         homingMissileScript2 = homingMissileObject2.GetComponent<HomingMissile2>();
     }
 
 
     private void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "Player 1")
         {
             StartCoroutine("SpawnMissile1");
         }
         if (col.gameObject.tag == "Player 2") 
         {
             StartCoroutine("SpawnMissile2");
         }
     }
 
     IEnumerator SpawnMissile1()
     {
         homingMissileScript1.targetPlayer2 = true; 
         Instantiate(homingMissile1, gameObject.transform.position, gameObject.transform.rotation); 
 
         gameObject.GetComponent<SpriteRenderer>().enabled = false;
         yield return new WaitForSeconds(10f);
         Destroy(gameObject);
     }
     IEnumerator SpawnMissile2()
     {
         homingMissileScript2.targetPlayer1 = true;  
 
         Instantiate(homingMissile2, gameObject.transform.position, gameObject.transform.rotation); 
 
         gameObject.GetComponent<SpriteRenderer>().enabled = false;
         yield return new WaitForSeconds(10f);
         Destroy(gameObject);
     }
 }
 

[This is one of the homingmissileScripts (for the Player1) the script for player2 homingmissile looks the same but with for example: "Transform Player2Transform" instead set to "Transform Player1Transform" ]

 [RequireComponent(typeof(Rigidbody2D))] 
 
 public class HomingMissile1 : MonoBehaviour
 {
     public float coolDown;
     public float lifeTime = 10f;
     public float speed;
     public float rotateSpeed;
 
     Transform Player2Transform;
 
     private Rigidbody2D rb2d;
 
     public bool targetPlayer2 = false;
     public bool canKill = false;
 
     // Use this for initialization
     void Start ()
     {
         Player2Transform = GameObject.FindGameObjectWithTag("Player 2").transform;
         rb2d = GetComponent<Rigidbody2D>();
     }
     
     // Update is called once per frame
     void FixedUpdate ()
     {
         if (targetPlayer2 == true)
         {
             StartCoroutine("Missile2");
         }
     }
 
     public void Update()
     {
         if(coolDown >= 0)
         {
             coolDown -= Time.deltaTime;
             if(coolDown <= 0)
             {
                 canKill = true;
             }
         }
     }
 
     IEnumerator Missile2()
     {
         rb2d.velocity = transform.up * speed;
         Vector2 direction = (Vector2)Player2Transform.position - rb2d.position; 
 
         direction.Normalize(); 
 
         float rotateAmount = Vector3.Cross(direction, transform.up).z;
         rb2d.angularVelocity = -rotateAmount * rotateSpeed;
         yield return new WaitForSeconds(lifeTime);
         targetPlayer2 = false;
         Destroy(gameObject);
     }
 
 }

[And lastly my script for the Player2 when they get hit by the homingmissile from player1, same goes the other way around for player1]

 public class Player2_Dead : MonoBehaviour {
 
     Player2_Controller controllerScript2;
     HomingMissile2 homingMissileScript2;
     HomingMissile1 homingMissileScript1;
 
     public GameObject hominMissile;
 
     public bool isKilled = false;
     public bool isKilled_M = false;
 
     private void Start()
     {
         controllerScript2 = gameObject.GetComponent<Player2_Controller>();
         homingMissileScript2 = hominMissile.gameObject.GetComponent<HomingMissile2>();
         homingMissileScript1 = hominMissile.gameObject.GetComponent<HomingMissile1>();
     }
 
     private void Update()
     {
     }
 
     private void OnCollisionEnter2D(Collision2D col)
     {
         
 
         if (col.gameObject.tag == "Shot 1")
         {
             StartCoroutine("KilledDelay");
         }
     }
 
     private void OnTriggerEnter2D(Collider2D col)
     {
         if (col.gameObject.tag == "Missile" && homingMissileScript1.canKill == true)
         {
             isKilled_M = true;
             gameObject.SetActive(false);
         }
     }
 
     IEnumerator KilledDelay()
     {
         if (controllerScript2 != null)
         {
             controllerScript2.enabled = false;
         }
         yield return new WaitForSeconds(2f);
         gameObject.SetActive(false);
         isKilled = true;
     }
 }

I'm thankful for every bit of help I can get with this!

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

0 Replies

· Add your reply
  • Sort: 

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

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

How to force a value stored in variable? 2 Answers

Cooldown Function Problem 1 Answer

Adding a Cooldown to a C# script 2 Answers

Moving a gameobject x time every x time 1 Answer

Get Component Error 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