- Home /
Question by
ChunkyDoge · Aug 05, 2020 at 07:57 PM ·
damagehowhealth
How does the player take damage if collided with enemy?
For now i use N to take damage.
public int maxHealth = 100;
public int currentHealth;
public HealthBarScript healthbar;
public GameObject fpsCam;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
TakeDamage(20);
}
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
}
}
Comment
Answer by Llama_w_2Ls · Aug 06, 2020 at 07:47 AM
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.CompareTag("Enemy"))
{
TakeDamage(20);
}
}
The player or the enemy must have a rigidbody on at least one of them to detect collision Should help with what youre looking for.
If i collide with a gameobject/rigidbody with the tag 'Enemy', then take damage
Your answer
Follow this Question
Related Questions
Health Question 1 Answer
Damage script 0 Answers
From script A in script B 1 Answer
Help w/variables 0 Answers