Lose Life when Hit Ground - c#

I was trying to create something like a lava floor in Unity 3D, like when the player falls and hit the ground they start losing life gradually, but my code does nothing.
Can someone help please?
public GameManager manager;
public Slider HealthBar;
public float Health = 100f;
public GameObject Plane;
public float Dmg = 10f;
void Start()
{
HealthBar.maxValue = Health;
HealthBar.value = Health;
}
void Update()
{
}
void OnCollisionEnter(UnityEngine.Collision collision)
{
if (collision.gameObject.tag == "LavaGround")
{
Health -= Dmg;
}
public void TakeDamage(float amnt)
{
Health -= amnt;
if (Health <= 0f)
{
manager.GameOver();
}
float _h = Mathf.Clamp(Health, 0f, 100f);
HealthBar.value = _h;
}

seems that nothing is happening because you are just adjusting the Health value in the
if (collision.gameObject.tag == "LavaGround")
{
Health -= Dmg;
}
Please try to change it as
if (collision.gameObject.tag == "LavaGround")
{
TakeDamage(Dmg);
}
to call TakeDamage method and check if health is below zero and update health bar.

Related

Player now does not jump after adding health in Unity

so first time learning C# for my team studio class. I found this video which I was able to get my player moving and jumping perfectly, https://www.youtube.com/watch?v=3GtQ2yQX2kU&t=569s however after I added a health/can die system to the script, now the player refuses to jump no matter what I do. Since I am a newbie at this, I am just going to post the whole thing here if anyone can point me in the right direction. I've played with the box colliders, added the layers to the platforms like the tutorial said... please, if anyone can help. I know I'm a newbie to this :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public HealthBar healthBar; //Communicates with the healthbar script
public float moveSpeed;
public float jumpForce;
public int jumpsAmount;
int jumpsLeft;
public Transform GroundCheck;
public LayerMask GroundLayer;
public int maxHealth = 3;
public int currentHealth;
bool isGrounded;
float moveInput;
Rigidbody2D rb2d;
float scaleX;
// Start is called before the first frame update
void Start()
{
healthBar.SetMaxHealth(maxHealth); //Sets healthbar to max health
currentHealth = maxHealth;
rb2d = GetComponent<Rigidbody2D>();
scaleX = transform.localScale.x;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0) //When Health is zero the player is destroyed
{
Destroy(gameObject);
}
healthBar.SetHealth(currentHealth); //Updates Healthbar when damgage is taken
}
// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");
Jump();
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
Flip();
rb2d.velocity = new Vector2(moveInput * moveSpeed, rb2d.velocity.y);
}
public void Flip()
{
if (moveInput > 0)
{
transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);
}
if (moveInput < 0)
{
transform.localScale = new Vector3((-1) * scaleX, transform.localScale.y, transform.localScale.z);
}
}
public void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
CheckIfGrounded();
if (jumpsLeft > 0)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
jumpsLeft--;
}
}
}
public void CheckIfGrounded()
{
isGrounded = Physics2D.OverlapCircle(GroundCheck.position, GroundCheck.GetComponent<CircleCollider2D>().radius, GroundLayer);
ResetJumps();
}
public void ResetJumps()
{
if (isGrounded)
{
jumpsLeft = jumpsAmount;// jumpsAmount =2;
}
}
}
I put the groundcheck collider where the video wanted me to, even played around having different locations too. Nothing is working... tried changing the jump force too to see if it needs to be higher. I am at a complete loss.

Why is my function runs twice in OnTriggerStay?

I have a simple game in Unity.
The situation is like if I kill an enemy, it drops a pickupable Health.
In the OnTriggerEnter, I handle the player collision with the health, and if the player misses health, it heals a certain amount. (70 exactly)
When player is standing on the health while at maximum health, it does nothing. But if player got damaged while standing on the health, the health should also be picked up, that is where OnTriggerStay comes in.
There I constantly check if the player needs heeling, if do, do the healing, and destroy the heal object.
My problem is that it runs twice. It heals the amount twice to the player. No matter what I do, the function I call in OnTriggerStay is gonna run twice. Why is that? Anybody knows the solution?
Here is the part of my Player.cs file, and my Heal.cs file:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
//HP and MANA system
[SerializeField]
private int playerHealth;
[SerializeField]
private int playerMaxHealth = 100;
[SerializeField]
private Transform Healthbar;
private void OnTriggerStay(Collider other)
{
if (other.tag == "Enemy")
{
timer += Time.deltaTime;
if (timer > attackSpeed)
{
Enemy enemy = other.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(playerDamage);
}
timer = 0.0f;
}
}
if (other.tag == "PatrollingEnemy")
{
timer += Time.deltaTime;
if (timer > attackSpeed)
{
PatrollingEnemy enemy = other.GetComponent<PatrollingEnemy>();
if (enemy != null)
{
enemy.DamageEnemy(playerDamage);
}
timer = 0.0f;
}
}
if (other.tag == "Heal")
{
heal(other, false);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Heal")
{
heal(other, false);
}
else if (other.tag == "HealthPotion")
{
heal(other, true);
}
}
private void heal(Collider other, bool isPotion)
{
dynamic heal;
if (isPotion)
{
heal = other.GetComponent<HealthPotion>();
}
else
{
heal = other.GetComponent<Heal>();
}
if (playerHealth < playerMaxHealth && heal != null)
{
addHealthToPlayer(heal.HealAmount);
Destroy(other.gameObject);
}
}
private void addHealthToPlayer(int amount)
{
playerHealth += amount;
if (playerHealth > playerMaxHealth)
{
playerHealth = playerMaxHealth;
}
rescaleHealthBar();
}
private void rescaleHealthBar()
{
Healthbar.transform.localScale = new Vector3((float)playerHealth / (float)playerMaxHealth, 1.0f, 1.0f);
}
}
public class Heal : MonoBehaviour
{
float timer = 0.0f;
float destroyTimer = 0.0f;
float timeWhenDestroy = 15f;
Vector3 rotation = new Vector3(0, 45, 0);
private int healAmount = 70;
public int HealAmount
{
get
{
return healAmount;
}
}
void Update()
{
timer += Time.deltaTime;
destroyTimer += Time.deltaTime;
if (destroyTimer > timeWhenDestroy)
{
Destroy(this.gameObject);
}
transform.Rotate(rotation * Time.deltaTime);
if (timer < 1.0f)
{
transform.Translate(Vector3.up * Time.deltaTime);
}
if (timer > 1.0f)
{
transform.Translate(-Vector3.up * Time.deltaTime);
}
if (timer > 2.0f)
{
timer = 0.0f;
}
}
}
https://docs.unity3d.com/ScriptReference/Object.Destroy.html
Maybe both your events (Enter & Stay) are fired during that frame ?
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html
OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger. The function is on the physics timer so it won't necessarily run every frame.

How to make damage client sided in unity?

I'm converting a simple platformer game into a multiplayer game(my first ever attempt at a multiplayer game), and i have these objects that if the player touches them, the player gets damaged. I'm having slight problems with server registering damage that shouldn't be, so to avoid all future problems i wanted to make damage client-sided.
I want to make it so all damage is client sided and the clients health gets sent to the server.
Edit: Made question less confusing.
public class EnemyTrigger : MonoBehaviour
{
private PlayerStats player;
private CharacterController2D controller;
public int Damage;
public float horizontal;
public float vertical;
private void Start()
{
}
private void OnTriggerStay2D(Collider2D collider)
{
if (collider.tag == "Player" && collider.GetComponent<PlayerStats>().DamageTimer <= 0 && collider.GetComponent<PlayerStats>().isDashing == false)
{
player = collider.GetComponent<PlayerStats>();
controller = player.GetComponent<CharacterController2D>();
player.TakeDamage(Damage);
if (this.transform.position.x - player.transform.position.x > 0)
{
controller.KnockBackRight(horizontal);
}
else
{
controller.KnockBackLeft(horizontal);
}
controller.KnockBackDown(vertical);
}
}
}
public class PlayerStats : NetworkBehaviour {
[SyncVar] public int Health;
public GameObject BloodEffect;
public Transform HealthBar;
public float DefDamageTimer;
public float DamageTimer;
public bool isDashing;
public int facingDirection;
private Rigidbody2D m_Rigidbody2D;
private CharacterController2D controller;
public AnimationPlayer anim;
private void Awake()
{
m_Rigidbody2D = GetComponent<Rigidbody2D>();
controller = GetComponent<CharacterController2D>();
}
private void Update()
{
HealthBar.localScale = new Vector3(Health / 10f, 1f);
if (DamageTimer > 0)
{
DamageTimer -= Time.deltaTime;
}
CmdSendDashing(isDashing);
}
public void TakeDamage(int Damage)
{
Health -= Damage;
Instantiate(BloodEffect, transform.position, Quaternion.identity);
anim.Damaged();
Debug.Log("Player Has Taken " + Damage + " Damage");
DamageTimer = DefDamageTimer;
if (Health <= 0)
{
Destroy(gameObject);
}
CmdSendHealth(Health);
}
void CmdSendHealth(int health)
{
Health = health;
}
void CmdSendDashing(bool isdashing)
{
isDashing = isdashing;
}
}

Pause collider in Unity

I would like that collider is paused for few seconds at the beginning of the game. I'm using this script on object. Thanks.
public class PickUpObject : MonoBehaviour {
void OnCollisionEnter (Collision Col)
{
if (Col.gameObject.name== "Player")
{
Debug.Log("collision detected");
Destroy(gameObject);
}
}
}
Use a timer to check it.
public class PickUpObject : MonoBehaviour {
public float timer = 10f; //seconds
private void Update()
{
timer -= Time.deltaTime;
}
void OnCollisionEnter (Collision Col)
{
if (Col.gameObject.name== "Player" && timer <= 0f)
{
Debug.Log("collision detected");
Destroy(gameObject);
}
}
}
My prefered method would be to set a _startTime variable with the time the scene started and then use that to check against when needed rather than incremementing a value each frame in a Update method.
private float _delay = 2f;
private float _startTime;
private void Start()
{
_startTime = Time.time;
}
void OnCollisionEnter (Collision col)
{
if(Time.time - _startTime < _delay)
{
//Exit if the time passed is less than the _delay
return;
}
//Else run check against player
if (col.gameObject.name == "Player")
{
Debug.Log("collision detected");
Destroy(gameObject);
}
}
public class PickUpObject : MonoBehaviour {
public float delayTime = 2.0f;
public float currentTime = 0.0f;
private void Update()
{
currentTime += Time.deltaTime;
}
void OnCollisionEnter (Collision Col)
{
if (Col.gameObject.name== "Player" && currentTime > delayTime)
{
delayTime += currentTime;
Destroy(gameObject);
delayTime -= currentTime;
currentTime = 0.0f;
}
}
}
You can use https://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html
and check if it's bigger than 5 :D

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.

Categories