Hello,
Let’s say my player as a sword and when I left click he swing his sword and attack the enemy in front of him.
(I don’t click on the enemy, in fact I don’t have the cursor showing at all so using raycast isn’t an option).
I want now to damage that specific enemy and I don’t know how to get that enemy health in my combat script.
Any ideas?
Im assuming that each enemy has a different name and that each one has a script containing their individual health and stats. You could do this by attaching a script to your sword, and if it collides with an enemy, then do damage, as well as access their stats script. For example:
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.CompareTag("Enemy")) //If my sword collides with an enemy:
{
GameObject enemyThatIHit = collision.gameObject; //Stores the enemy's information into a local gameObject
HealthScript enemyHealth = enemyThatIHit.GetComponent<HealthScript>(); //Gets the enemy's health script
float HealthOfEnemy = enemyHealth.health; //Gets the enemy's health
}
}
Hope it works! @dddaniel159