When I attack I'm not sure why the enemy is not taking damage, my enemy script works i believe as when I set its health to 0 the enemy is deleted.
this is the script i believe has problems with it. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AttackScript : MonoBehaviour {
public float hp = 10f;
public float toTarget;
public float range = 5f;
// Update is called once per frame
void Update () {
Attack ();
}
void Attack(){
RaycastHit hit;
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 origin = transform.position;
if(Physics.Raycast(origin, forward, out hit, range)){
if(hit.transform.gameObject.tag == "Enemy"){
hit.transform.gameObject.SendMessage("TakeDamage", hp);
}
}
}
}
Here is my enemy script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour {
public int enemyHealth = 50;
void TakeDamage(int hp){
enemyHealth -= hp;
}
// Update is called once per frame
void Update () {
if (enemyHealth <= 0) {
Destroy (gameObject);
}
}
}
↧