我有一个小问题。
我有两个脚本:
问题本身是什么?
当对敌人造成伤害时,我不会占用一定数量的生命,敌人会立即死于一枪。 我不明白他为什么一枪就死了。当我让他做伤害杀人.... 请帮忙。 抱歉我的英语不好。
Bullet - 脚本(使用Method Damage(),HitTarget())。 该方法可能有问题损坏() - 我不知道。
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脚本中出错......
答案 0 :(得分:2)
因为您正在通过调用Update
调用HiTarget
来应用Damage
中的伤害,因此每个frame
都会调用它,并且您的敌人会快速死亡。
你可以设置一个boolean
来查看函数是否已被调用,如果没有调用它,那么在调用它之前将boolean设置为true然后调用它,就像这样只调用一次。
这不是最好的方法,但它会解决你的问题。
int called=false;
void Update () {
if (target == null)
{
Destroy(gameObject);
return;
}
if(!called){
checkForTarget()
}
}
void checkForTarget(){
Vector3 diraction = target.position - transform.position;
float distanceframe = speed *Time.deltaTime;
if(diraction.magnitude <= distanceframe)
{
called = true;
HiTarget();
return;
}
transform.Translate(diraction.normalized * distanceframe, Space.World);
transform.LookAt(target);
}