所以我正在制作FPS生存游戏。我制作了一个GameManager脚本来处理游戏中的Enemy Spawning部分。我的EnemyAttack脚本处理敌人的攻击。问题是,当第一个敌人产生时,我只受到1点伤害(在AttackDamage中设置),但是当第二个生成时,即使我只进入1个敌人的攻击范围,我也会受到2点伤害(AttackDamage * 2)。 以下是我所有的敌人脚本:
EnemyAttack.cs :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class EnemyAttack : MonoBehaviour
{
public float AttackDamage = 1.0f;
public float AttackSpeed = 1.0f;
void Start() {
StartCoroutine(Attack());
}
IEnumerator Attack() {
while (true)
{
while (PlayerMovement.isEnemyAttacking == true) {
EnemyAttacking();
yield return new WaitForSeconds(AttackSpeed);
}
yield return 0;
}
}
public void EnemyAttacking() {
Debug.Log("Enemy Attacking!");
PlayerHealth.Health -= AttackDamage;
GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>().text = "HEALTH: " + PlayerHealth.Health;
}
}
EnemyAI.cs :
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform TriggerBox;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (EnemySight.inSight == true)
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.destination = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>().position;
}
}
}
EnemySight.cs :
using UnityEngine;
using System.Collections;
public class EnemySight : MonoBehaviour {
public static bool inSight = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
inSight = true;
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "Player")
{
inSight = false;
}
}
}
和 PlayerHealth.cs :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public Transform Player;
public static float Health = 100;
public static float maxHealth = 100;
// Use this for initialization
void Start () {
GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>().text = "HEALTH: " + Health;
}
// Update is called once per frame
void Update () {
if (Health <= 0) {
Dead();
}
}
public static void Dead() {
}
}
答案 0 :(得分:2)
您的脚本存在一些问题......
我不认为你理解静态变量是什么。
静态变量在类的所有实例中共享它的值
public static bool inSight;
不应该是静态的。
PlayerMovement.isEnemyAttacking
这也不应该是静态的。
现在,只要你有1个敌人会见&#34;攻击&#34;条件,你正在设置和读取一个静态变量,导致所有敌人攻击。你应该让你的变量属于每个ennemy实例,而不是让变量变为静态。
基本上,您只需要了解静态变量是什么。一旦你知道,你就会理解为什么你的逻辑不起作用。
编辑:解决方案
PlayerHealth脚本。将此脚本附加到场景中的播放器GameObject。
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public static float Health;
public static float maxHealth = 100;
private Text healthText;
void Start()
{
healthText = GameObject.FindGameObjectWithTag("HealthPoints").GetComponent<Text>();
//Make it full 100% health on start
Health = maxHealth;
RefreshHealthBar();
}
public void TakeDamage(float damage)
{
Health -= damage;
RefreshHealthBar();
if (Health <= 0)
Die();
}
public void Die()
{
Health = 0;
RefreshHealthBar();
//TODO: Your code
}
void RefreshHealthBar()
{
healthText.text = "HEALTH: " + Health;
}
}
EnnemyAI脚本。将此附在你的Ennemy预制件上。
using System.Collections;
using UnityEngine;
public class EnnemyAI : MonoBehaviour
{
public float AttackDamage = 1.0f;
public float AttackSpeed = 1.0f;
public float AttackRange = 1.0f;
private bool isPlayerInSight;
private GameObject target;
private NavMeshAgent agent;
// Use this for initialization
void Start ()
{
target = GameObject.FindGameObjectWithTag("Player");
agent = GetComponent<NavMeshAgent>();
StartCoroutine(AttackLoop());
}
// Update is called once per frame
void Update ()
{
if (isPlayerInSight)
{
agent.destination = target.transform.position;
}
}
IEnumerator AttackLoop()
{
while (true)
{
//I don't know your attacking logic, so lets say they can attack in a 1 unit range
while (Vector3.Distance(target.transform.position, this.transform.position) <= AttackRange)
{
Attack();
yield return new WaitForSeconds(AttackSpeed);
}
yield return 0;
}
}
void Attack()
{
target.GetComponent<PlayerHealth>().TakeDamage(AttackDamage);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
isPlayerInSight = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
isPlayerInSight = false;
}
}
}
这样我们摆脱了静态变量。