- Home /
My script only partially works
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();
}
}
Answer by NSteffensen · Jun 09, 2020 at 06:41 PM
It seems the boolean values aren't being reset to their initial values upon death. Have you tried reassigning them in destroyEnemy()?
Answer by tadadosi · Jun 09, 2020 at 07:55 PM
I believe this script does what you are trying to achieve. Let me know if it works.
Edit: Changed the script to the final version that solved the issue.
using UnityEngine;
public class MyNewClass : MonoBehaviour
{
[SerializeField] private int hitMaxAmount = 2;
public LayerMask WhatCanIHit;
private const float checkHitDelay = 0.2f;
private float checkHitTime;
private bool isCheckHitTime;
private bool isCheckHit = true;
private int hitCount;
void Update()
{
// Only check for hits if true
if (isCheckHit)
{
// Use a layer mask to stop this raycast from hitting the base object
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector3.up, 5, WhatCanIHit);
Debug.DrawRay(transform.position, Vector3.up * 5f, Color.red);
// Check if it's hitting a collider
if (hitInfo.collider != null)
{
// Use CompareTag for better performance
if (hitInfo.collider.CompareTag("Player"))
{
Debug.Log("Hit count: " + hitCount);
isCheckHit = false;
isCheckHitTime = false;
hitCount++;
if (hitCount == 1)
{
Debug.Log("Got hit once, change color");
// Change Color
}
if (hitCount >= hitMaxAmount)
{
Debug.Log("Destroy Enemy");
// Destroy Enemy and stuff
// Reset hit count to let it run again
hitCount = 0;
}
}
}
}
if (!isCheckHitTime)
{
checkHitTime += Time.deltaTime;
if (checkHitTime >= checkHitDelay)
{
Debug.Log("You can hit again");
isCheckHitTime = true;
isCheckHit = true;
checkHitTime = 0.0f;
}
}
}
}
I should of left the comment in there in the script, sorry about that.
The timeDelay is in there so the enemy is destroyed right away. The higher the delay, the longer it takes after jumping over it. How I have it is fine and it works.
Problem is, after the enemy respawns, the amount of times jumped over the enemy is not reset back to zero, so jumping over it again after it repsawns destroys it without having to wait for the second jump.
I updated my answer based on your reply, I think that it does now what you are trying to achieve.
I tried it out and included my destroy enemy and change color functions in the script and neither works.
When testing the script out, it only reads the debug line from here, even after jumping over once, or twice:
if (!isCheckHitTime)
{
checkHitTime += Time.deltaTime;
if (checkHitTime >= checkHitDelay)
{
Debug.Log("You can hit again");
isCheckHitTime = true;
isCheckHit = true;
checkHitTime = 0.0f;
Your answer
Follow this Question
Related Questions
I really need a JumpPad script for a JumpPad in unity 2D 0 Answers
JumpPad script for a JumpPad like Geometery dash 1 Answer
Multiple Cars not working 1 Answer
Values not moving to Game Over screen or the Game over doesn't somehow work. 0 Answers
Unity|C#| Card Game: How to draw a card for each 0.5 secs? 2 Answers