В Unity3D мой враг не наносит урон при столкновении с моим взрывом снаряда.Unity3D - враг не получает урона
Хотя это не так, поскольку переменная здоровья не влияет на столкновение с моим взрывом снаряда.
Мои Enemy
и Barrel
классы наследуют от Entity
, который обрабатывает взятие урона (вычитая переменную ущерба из переменной работоспособности). Хотя только класс ствола работает по назначению.
Теги на 100% правильные, и я предпочел бы продолжать использовать наследование, поэтому, пожалуйста, не предлагайте изменить метод, в котором мои классы получают урон.
класс, Enemy
и Barrel
унаследуют от
using UnityEngine;
using System.Collections;
public class Entity : MonoBehaviour {
public float health = 25;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
}
public virtual void takeDamage(float dmg){
health -= dmg;
if (health <= 0){
Destroy(this.gameObject);
}
}
}
Enemy
класс
using UnityEngine;
using System.Collections;
public class Enemy : Entity {
private NavMeshAgent agent;
public GameObject target;
// Use this for initialization
void Start() {
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update() {
agent.SetDestination (target.transform.position);
}
}
Barrel
класс
using UnityEngine;
using System.Collections;
public class Barrel : Entity {
private Transform myTransform;
//Effects
public GameObject barrelExplosion;
public GameObject explosionDamage;
public GameObject explosionSound;
// Use this for initialization
void Start() {
myTransform = this.transform;
}
// Update is called once per frame
void Update() {
}
public override void takeDamage(float dmg){
health -= dmg;
if (health <= 0){
Instantiate(barrelExplosion, myTransform.position, myTransform.rotation);
Instantiate(explosionSound, myTransform.position, myTransform.rotation);
Instantiate(explosionDamage, myTransform.position, myTransform.rotation);
Destroy(this.gameObject);
}
}
}
ExplosionAOE
класс, который посылает ущерб
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ExplosionAOE : MonoBehaviour {
public float damage = 100.0f;
public float lifeTime = 0.05f;
private float lifeTimeDuration;
public List<GameObject> damageTargets = new List<GameObject>();
public float radius = 15.0f;
GameManager gameManager;
void Start() {
gameManager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
//Destroy (this.gameObject, lifeTime);
lifeTimeDuration = Time.time + lifeTime;
transform.GetComponent<SphereCollider>().radius = radius;
}
void Update() {
//Explosion finishes, damage targets and remove AOE field
if (Time.time > lifeTimeDuration) {
foreach (GameObject target in damageTargets) {
if (target != null) {
//Calculate damage based on proximity to centre of explosion
float thisDamage = ((radius - Vector3.Distance(target.transform.position, transform.position))/radius) * damage;
print(thisDamage);
target.GetComponent<Entity>().takeDamage(thisDamage);
//target.SendMessage("takeDamage", damage); //<< This is not good code. Let's fix this!
}
}
Destroy(this.gameObject);
}
}
void OnTriggerEnter(Collider otherObject) {
if (otherObject.gameObject.tag == "Enemy") {
damageTargets.Add(otherObject.gameObject);
}
if (otherObject.gameObject.tag == "Player") {
Vector3 jumpVector = (otherObject.transform.position - transform.position).normalized;
jumpVector *= 25;
otherObject.GetComponent<CharacterMotor>().SetVelocity(jumpVector);
}
}
}
Извините, что это немного длинный, и все равно помечены правильно, так что это не проблема, спасибо.
просто любопытно: есть ли причина для наследования из другого класса вместо использования интерфейса типа 'IHittable'? – garglblarg
@garglblarg Я не уверен, я студент, изучающий программирование игры в uni, и это именно то, как нас учили делать. – jozza710
Что именно не работает? не влияет ли ценность здоровья вашего врага при вызове 'takeDamage'? –