【发布时间】:2016-10-27 00:25:18
【问题描述】:
在 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 我不确定,我是一名在 uni 学习游戏编程的学生,这正是我们被教导的方式。
-
@jozza710 - 注意你在哪里说
gameManager = ...在 Unity 中你通常使用不同的习语。只是FindObjectOfType请转到this answer 并向下滚动到“幸运的是没问题...”您的代码看起来非常好,大多数在这里发帖的学生都是垃圾。 -
@JoeBlow 谢谢,我使用了你建议的使用
Debug.Log的方法(我最初应该采用的方法)并发现这实际上是因为我的敌人没有刚体,我'我不知道为什么这会阻止我的敌人受到伤害你能向我解释一下为什么需要刚体才能让敌人受到伤害吗? -
@jozza710 刚体 .. 当然,这里有充分的解释:docs.unity3d.com/Manual/CollidersOverview.html 转到“触发动作矩阵”。这是 Unity 的一个非常烦人的方面。每次我使用对撞机和/或触发器时,我都必须参考该图表!不可能记住它或清楚地看到它为什么会这样工作。注意请不要忘记,您绝对必须在碰撞物理中使用 layers。
标签: c# inheritance unity3d