Unity Take damage while colliding - c#

Hey i am making a zombie game and i want everytime the zombie colides with my player it should deal damage every 2 seconds as long as they are colliding it should continue. while not colliding it should stop. what i did is this code bellow and the problem with it is that when i collide with the zombie it does damage once and stops i need to collide with it again for it to deal damage can anyone help so the zombie deals damage as long as they are colliding and the damage should be dealt every 2 seconds, thanks for the help :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public float Health = 100f;
public bool gameOver;
private Animator playerAnim;
float timeColliding;
// Start is called before the first frame update
private void Start()
{
playerAnim = GetComponent<Animator>();
}
private void Update()
{
if (Health <= 0)
{
playerAnim.SetBool("PlayerDeath", true);
gameOver = true;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
Debug.Log("Enemy started colliding with player.");
this.Health -= 10;
}
}
}

I'm not good at unity, but i think something like this will work. Place all code inside while loop in my code to function in your code called to detect collision. And place isEven outside that function. You can test my code in c# compiler. For example if you replace //reduce health here with Console.WriteLine("reduced"); this code will print reduced every 2 second
public class Program
{
public static void Main()
{
bool isEven = false;
while(true){
var timeSpan = DateTime.Now;
if(timeSpan.Second %2 == 0){
if(isEven == false){
//reduce health here
}
isEven = true;
}
else{
isEven = false;
}
}
}
}
In your code it will look like this:
public class PlayerHealth : MonoBehaviour
{
bool isEven = false;
public float Health = 100f;
public bool gameOver;
private Animator playerAnim;
float timeColliding;
// Start is called before the first frame update
private void Start()
{
playerAnim = GetComponent<Animator>();
}
private void Update()
{
if (Health <= 0)
{
playerAnim.SetBool("PlayerDeath", true);
gameOver = true;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
Debug.Log("Enemy started colliding with player.");
var timeSpan = DateTime.Now;
if(timeSpan.Second %2 == 0){
if(isEven == false){
//reduce health here
this.Health -= 10;
}
isEven = true;
}
else{
isEven = false;
}
}
}
}

Related

Game Freezes When Ever Lives Get Removed In Unity [duplicate]

This question already has answers here:
How to make the script wait/sleep in a simple way in unity
(7 answers)
Closed 1 year ago.
Whenever lives get removed by an enemy in my game in unity, the game temporarily freezes. I think it has to do with the line "Thread.Sleep(3000);" but I am not sure of an alternative wait that will not freeze the entire game temporarily.
I would greatly appreciate if you could help me!
Thanks.
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform attackPoint;
public float attackrange = 0.5f;
public LayerMask playerlayers;
public float speed = 3f;
private Transform target;
bool hitID = false;
private void Update()
{
if (target != null)
{
float step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, target.position, step);
Collider2D[] hitplayers = Physics2D.OverlapCircleAll(attackPoint.position, attackrange, playerlayers);
foreach (Collider2D player in hitplayers)
{
if (hitID == false)
{
hitID = true;
player.GetComponent<HeartSystem>().TakeDamage(1);
Thread.Sleep(3000);
hitID = false;
}
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
target = other.transform;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
target = null;
}
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackrange);
}
}
Enemy.cs
using System.Collections;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform attackPoint;
public float attackrange = 0.5f;
public LayerMask playerlayers;
public float speed = 3f;
private Transform _target;
private bool _hitID;
private void Update()
{
if (_target != null)
{
var step = speed * Time.deltaTime;
transform.position = Vector2.MoveTowards(transform.position, _target.position, step);
var hitPlayers = new Collider2D[10];
Physics2D.OverlapCircleNonAlloc(attackPoint.position, attackrange, hitPlayers, playerlayers);
foreach (var player in hitPlayers)
{
if (_hitID == false)
{
StartCoroutine(HitCoroutine(player));
}
}
}
}
private IEnumerator HitCoroutine(Collider2D player)
{
_hitID = true;
player.GetComponent<HeartSystem>().TakeDamage(1);
yield return new WaitForSeconds(3);
_hitID = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
_target = other.transform;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
_target = null;
}
}
private void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackrange);
}
}
Try this.
In a case like this you want to use a coroutine.
You can read about coroutines here:
https://docs.unity3d.com/Manual/Coroutines.html
Unity is single threaded, so you are actually suspending the entire thread the game is running on. Since the frames are updated on this thread, you're even freezing the visual frame updates.
You have a few ways to manage "waiting" in game.
Coroutines:
A process that can be suspended until a later condition is met (including time)
void Update()
{
//Your other code goes here
if (hitID == false)
{
hitID = true;
player.GetComponent<HeartSystem>().TakeDamage(1);
StartCoroutine(ResetHitIDAfterSeconds(3))
}
}
IEnumerator ResetHitIDAfterSeconds(float seconds)
{
yield return new WaitForSeconds(seconds);
hitID = false;
}
The above code allows you to have the same logic, and will kick off a coroutine that waits for 3 seconds before setting hitID back to false.
Time.deltaTime:
You can also track the elapsed time in your Update method and manage your variables actively based on the elapsed time. Time.deltaTime returns the amount of time that elapsed since the previous frame.
public class ConstantRotation : MonoBehaviour
{
public float timeElapsed = 0f;
void Update()
{
//Your other code goes here
if (hitID == false)
{
hitID = true;
player.GetComponent<HeartSystem>().TakeDamage(1);
timeElapsed = 0f;
}
else
{
timeElapsed += Time.deltaTime
if(timeElapsed >= 3.0f)
{
hitId = false;
}
}
}
}
You would likely want to modify the above code a bit to avoid having the logic run on every frame, and only run it on hit.

How to check for collision in an if statement in Unity 2D

I have a script that counts down from 20 to 0, then when it reaches 0 a boolean is set to true. What I want that boolean to do is write something on the console when the player is collided with the sprite the script is on, but I only want this to happen if it's true. Here's the code:
public float timeLeft = 10f;
public bool canHarvest;
public void Start()
{
canHarvest = false;
}
public void Update()
{
timeLeft -= Time.deltaTime;
if (timeLeft < 0)
{
Debug.Log("IM READY TO HARVEST");
canHarvest = true;
}
if (canHarvest = true)
{
public void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
Debug.Log("true");
}
}
}
}
}
What I want this code to do is set canHarvest to false at the start (meaning that if the player collides with the object the script is on nothing will happen), and once timeLeft reaches 0 it should set canHarvest to true, meaning that if the player collides with the object the script is on, the object the script is on will disappear. This is not working as I can't have the if statement checking if the player collides inside the if statement checking if canHarvest is true. Please help!
You should move the OnTriggerEnter2D definition outside the Update() method and conditional check should be inside the function:
public float timeLeft = 10f;
public bool canHarvest;
public void Start()
{
canHarvest = false;
}
public void Update()
{
timeLeft -= Time.deltaTime;
if (timeLeft < 0)
{
Debug.Log("IM READY TO HARVEST");
canHarvest = true;
}
}
public void OnTriggerEnter2D(Collider2D col)
{
if (canHarvest && col.gameObject.tag == "Player")
{
Debug.Log("true");
}
}

Object is being spammed when it comes to my timeBetweenShot variable, can anyone take a look?

I Have created a 2d stealth game where the enemy fires on the player, only problem is that although the bullets are created and deleted fine on another script, the script itself spams the program with bullets every frame, creating the unwanted result
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HurtPlayer : MonoBehaviour
{
public float timeToShoot;
private float timeToShootCounter;
private bool shot;
private Vector3 moveDirection;
public float timeBetweenShot;
public float timeBetweenShotCounter;
public Transform firePoint;
public GameObject Bullet;
// Use this for initialization
void Start()
{
shot = false;
timeToShootCounter = timeToShoot;
}
// Update is called once per frame
void Update()
{
while (shot == true)
{
StartCoroutine(Delay());
Destroy(GameObject.Find("Bullet"));
timeBetweenShot -= Time.deltaTime;
timeToShoot -= Time.deltaTime;
}
}
IEnumerator Delay()
{
yield return new WaitForSeconds(0.5f);
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "player")
{
if (shot == false)
{
if (timeToShoot >= 0f)
{
shot = true;
if (shot == true)
{
shot = false;
Instantiate(Bullet, firePoint.position, firePoint.rotation);
Delay();
if (timeBetweenShot <= 0f)
{
shot = false;
timeToShoot = timeToShootCounter;
timeBetweenShot = timeBetweenShotCounter;
}
}
}
}
}
}
}
What I want is the time betweenshot to work and for the enemy to only shoot once every one or half a second, thanks.
Is this what you are looking for?
IEnumerator ContinuousShoot()
{
// Continuously spawn bullets until this coroutine is stopped
// when the player exits the trigger.
while (true)
{
yield return new WaitForSeconds(1f); // Pause for 1 second.
Instantiate(Bullet, firePoint.position, firePoint.rotation);
}
}
void OnTriggerEnter2D(Collider2D other)
{
// Player enters trigger
if (other.gameObject.CompareTag("player"))
{
StartCoroutine(ContinuousShoot());
}
}
void OnTriggerExit2D(Collider2D other)
{
// Player exits trigger
if (other.gameObject.CompareTag("player"))
{
StopCoroutine(ContinuousShoot());
}
}

Cause player to take damage in Unity 2D

I made two scripts. One that'll keep track of the player's health, health bar and cause the screen to flash when the player is damaged. The other script is meant to be placed on any object I wish to do damage to the player, on contact. My problem is, Nothing seems to be doing any damage to the player.
PlayerHealth.cs:
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int currentHealth;
public float flashSpeed = 5;
public Slider healthSlider;
public Color flashColour = new Color(1, 0, 0, 0.1f);
bool isDead;
bool damaged;
private void Awake()
{
currentHealth = 100;
}
private void Update()
{
damaged = false;
}
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
}
}
AttackPlayer.cs:
using UnityEngine;
public class AttackPlayer : MonoBehaviour
{
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
GameObject player;
PlayerHealth playerHealth;
float timer;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject == player)
{
Attack();
}
}
private void Update()
{
timer += Time.deltaTime;
if(playerHealth.currentHealth <=0)
{
// TODO: add death script here.
}
}
void Attack()
{
timer = 0f;
if(playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
}
The player has a rigidbody2D. The player and damaging objects have Box Collider 2D's on them.
Make sure that the players Collider has isTrigger enabled.
attackDamage is public -> set in the inspector. Make sure it is not 0.
You could use
[Range(1,100)] public int attackDamage = 10;
to automatically clamp the value in the inspector.
A guess but I'ld say your Collider might not be on the GameObject player but probably on one of its children => the condition col.gameObject == player is not true.
Instead of GameObject references rather compare the PlayerHealth (since there is only one) reference like
private void OnTriggerEnter2D(Collider2D col)
{
// gets PlayerHealth component on this or any parent object
var health = col.GetComponentInParent<PlayerHealth>();
if (health == playerHealth)
{
Attack();
}
}
You have
private void Update()
{
damaged = false;
}
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
}
I don't know what else should happen on TakeDamage but the value of damaged is resetted in Update so right after it was set by the Trigger because Physics events like OnTriggerEnter are executed before Update (see execution Order).
Hint: Instead of
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
you could also use
playerHealth = FindObjectOfType<PlayerHealth>();
if that component exists only once in your scene.
Or to be more flexible (having multiple Players) all you have to do is change your OnTriggerEnter2D and Attack method to
private void OnTriggerEnter2D(Collider2D col)
{
// gets PlayerHealth component on this or any parent object
var health = col.GetComponentInParent<PlayerHealth>();
if (health != null)
{
Attack(health);
}
}
void Attack(PlayerHealth health)
{
timer = 0f;
if(health.currentHealth > 0)
{
health.TakeDamage(attackDamage);
}
}
So you wouldn't need to get the reference before.

An object reference is required to access non-static field in C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject Player;
PlayerHealth playerHealth;
//EnemyHeath enemyhealth;
bool playerInRange;
float timer;
private Animator animator = null;
void Awake() {
Player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = Player.GetComponent<PlayerHealth> ();
//enemyHealth = GetComponent<EnemyHealth> ();
anim = GetComponent <Animator> ();
}
void Attack(Collider other) {
if (other.gameObject == Player) {
playerInRange = true;
animator.SetBool ("idle0ToAttack1", true);
}
}
void Attack1(Collider other) {
if (other.gameObject == Player) {
playerInRange = false;
}
}
void Update() {
timer +=Time.deltaTime;
if (timer >= timeBetweenAttacks /*&& enemyHealth.currentHealth > 0*/) {
AttackPlayer ();
}
if (playerHealth.currentHealth <= 0) {
Destroy (this.Player);
}
}
void AttackPlayer() {
timer = 0f;
if (PlayerHealth.currentHealth > 0) {
playerHealth.TakeDamage (attackDamage);
}
}
}
The error is giving on last method void AttackPlayer() if(PlayerHealth.currentHealth > 0).
I am making a first Person Shooter Game in Unity and if possible please tell give me some more suggestions for player dead animator code that I've written above.
void AttackPlayer()
{
timer = 0f;
if(playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
I've corrected the code. It was a simple mistake of using PlayerHealth instead of what you meant which was playerHealth. Using the uppercase was referring to the class itself. The lowercase would refer to your current object defined earlier in this class.

Categories