- Home /
Question by
TvattSvampen · Oct 13, 2018 at 07:39 AM ·
c#unity 5playerenemyenemydamage
Enemy wont damage player
Hey, im kinda a beginner so i dont understand complex stuff. im trying to make a game where the enemy will damage the player when it walks inside of my box collider The enemy has a box collider set to trigger This is my scripts:
Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour {
public bool inRange;
public float damage;
void Start ()
{
}
void Update ()
{
if(inRange == true)
{
//attack
PlayerStats player = GetComponent<PlayerStats>();
player.P_TakeDamage(damage);
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
inRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
inRange = false;
}
}
}
Player Script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerStats : MonoBehaviour {
public float currHealth;
public float maxHealth;
void Start ()
{
}
public void P_TakeDamage(float ammount)
{
currHealth -= ammount;
}
void Update ()
{
if (currHealth > maxHealth)
{
currHealth = maxHealth;
}
if (currHealth <= 0)
{
//Die
Die();
}
}
public void Die()
{
Destroy(gameObject);
}
}
Comment
Your answer
Follow this Question
Related Questions
Enemy wont damage player 2 Answers
Enemy won't damage player C# 1 Answer
Run coroutine only when player stays inside collider? 0 Answers
Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer
Distribute terrain in zones 3 Answers