【发布时间】:2018-05-18 20:46:41
【问题描述】:
我有一个小问题。
我有 2 个脚本:
- 这是敌人(敌人)
- 这是子弹(子弹)
问题本身是什么?
对敌人造成伤害时,我不取一定数量的生命,一击必杀敌人。 我不明白为什么他会死于一枪。当我把他做伤害杀.... 请帮忙。 对不起我的英语不好。
Bullet - 脚本(使用 Method Damage() 、 HitTarget())。 该方法可能有问题 Damage() - 我不知道。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour {
private Transform target;
public GameObject impactEffect;
public float speed = 70f;
public int damage = 50;
public void Seek(Transform itarget)
{
target = itarget;
}
// Update is called once per frame
void Update () {
if (target == null)
{
Destroy(gameObject);
return;
}
Vector3 diraction = target.position - transform.position;
float distanceframe = speed *Time.deltaTime;
if(diraction.magnitude <= distanceframe)
{
HiTarget();
return;
}
transform.Translate(diraction.normalized * distanceframe, Space.World);
transform.LookAt(target);
}
void HiTarget()
{
GameObject effect = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
Destroy(effect, 1f);
Damage(target);
// Destroy(gameObject);
}
void Damage(Transform enemy)
{
Enemy e = enemy.GetComponent<Enemy>();
if (e != null)
{
e.TakeDamage(damage);
}
//Destroy(enemy.gameObject);
}
}
敌人 - 脚本(使用方法 TakeDamage() , Die() )
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public float speed = 10f;
private int health;
public int startHealth = 100;
private bool isDead = false;
private Transform target;
private int waveWayPointIndex = 0;
void Start()
{
health = startHealth;
target = Waypoints.waypoint[0];
}
public void TakeDamage(int amount)
{
health -= amount;
if (health <= 0 && !isDead)
{
Die();
}
}
void Die()
{
isDead = true;
Destroy(gameObject);
}
void Update()
{
Vector3 diraction = target.position - transform.position; //от одной
позиции мы поворачиваемся к другой
transform.Translate(diraction.normalized * speed *
Time.deltaTime,Space.World); // переводим со скоростью
if (Vector3.Distance(transform.position,target.position)<= 0.4f)
{
NextWayPoint();
}
}
void NextWayPoint()
{
if(waveWayPointIndex >= Waypoints.waypoint.Length - 1 )
{
EndPath();
return;
}
waveWayPointIndex++;
target = Waypoints.waypoint[waveWayPointIndex];
}
void EndPath()
{
PlayerStat.Lives--;
Destroy(gameObject);
}
}
我更倾向于 Bullet 脚本中的错误...
【问题讨论】:
-
你能在游戏运行的情况下在 Unity 中调试这个脚本吗?如果是这样,请在
TakeDamage上设置一个断点。调用TakeDamage时health和amount的值是多少? -
看起来你想在调用 HiTarget 后销毁子弹对象,否则你会造成持续的伤害。