Jumping over enemy once changes color, jumping over it a second time destroys the enemy, then the enemy re-spawns, those parts work. The problem is, the number of times jumped doesn’t reset back to zero, so after the enemy re-spawns jumping over the enemy once destroys it. Revering to starting color after enemy is destroyed works too.
How do I do it so you need to jump over an enemy twice each time it re-spawns?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Destroy enemy 2 after jumping twice, jumping once turns him to blue
public class DestroyEnemy2 : MonoBehaviour //enemy 2
{
[SerializeField] Transform Enemy2Respawn;
private Renderer rend;
private Color colorToTurnTo = Color.blue;
private Color colorToTurnToo = Color.grey; // after destroying enemy, changes color back to original
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
Physics2D.queriesStartInColliders = false; //since ray starts from the enemy...
//...game object, this will make it so it won't...
//...destroy itself right when the game starts
}
bool waitingForFirstHit = true;
bool waitingForSecondHit = false;
float timeDelay = 0.2f;
void Update()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5);
if (hitInfo.collider.gameObject.tag == "Player")
{
if (waitingForFirstHit)
{
ChangeColor();
waitingForFirstHit = false;
waitingForSecondHit = true;
}
else if (waitingForSecondHit && timeDelay < 0)//when it's >0 it causes it to be destroyed after one jump. when its <0 , it just changes color and waits till you do a second jump
{
destroyEnemy();
transform.position = Enemy2Respawn.position;
Score.scoreValue += 50;
}
}
if (waitingForSecondHit)
{
timeDelay -= Time.deltaTime;
}
}
void ChangeColor()
{
rend.material.color = colorToTurnTo;
}
void ChangeColorr() //turn back to original after destroying enemy
{
rend.material.color = colorToTurnToo;
}
void destroyEnemy()
{
{
Destroy(GameObject.FindWithTag("Enemy2"));
ChangeColorr();
}
}