- Home /
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!
Your answer
Follow this Question
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