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; }
}