Damage manager (ThirdPersonShooter)
Hi, I have problem with getting value from another script which is in another object. My result right now is almost correct but i done something wrong, cuz damage receives on second tick.
Result below:
Fourth and fifth values are the damage that the enemy deals to the player on collision. I have to fix it in such a way that it detects the damage dealt immediately.
Damage_manager below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class damage_manager : MonoBehaviour
{
//public float health;
public static float damage;
private float armor_dmg;
private float health_dmg;
void Update()
{
//if (health <= 0)
//{
// Destroy(gameObject);
//}
}
void OnCollisionEnter(Collision hit)
{
armor_dmg = damage * 0.7f;
health_dmg = damage * 0.3f;
player_mechanics stats = GetComponent<player_mechanics>();
if (hit.gameObject.tag == "Enemy")
{
Debug.Log(stats.Armor + " " + stats.Health + " " + health_dmg + " " + armor_dmg);
if (stats.Armor > 0)
{
if (stats.Armor >= armor_dmg)
{
stats.Armor -= armor_dmg;
stats.Health -= health_dmg;
}
else
{
stats.Health -= ((armor_dmg - stats.Armor) * 0.5f + health_dmg);
stats.Armor = 0;
}
}
else
{
stats.Health -= damage;
}
if (stats.Health < 0)
{
stats.Health = 0;
}
}
}
}
I think I am making a mistake in my enemy's script.
Script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class damage_test : MonoBehaviour
{
public float Damage_value;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Debug.Log("Sprawdź kolizje - test1");
damage_manager.damage = Damage_value;
}
if (collision == null) { return; }
}
Answer by AbandonedCrypt · Feb 12, 2021 at 02:56 PM
Class names should never have underscores and should be written in PascalCase (not class_name but ClassName)
variable names should not have underscores, they should be written in camelCase.
I have no idea where you fetch
damage_manager
from in your 2nd scriptYour code in damage_manager only executes when a collision is happening, and it is executed by a collision, so basically a collision makes it wait for a collision to execute code.
You are setting a damage value directly, you should not do this.
What you could do is:
implement a methodDamage(float damageAmount)
that executes the damage logic in DamageManager, and then call that method from the collider (i assume the damage manager works this way, it's all very untransparent)
Your answer
![](https://koobas.hobune.stream/wayback/20220613025046im_/https://answers.unity.com/themes/thub/images/avi.jpg)