我一直在使用C#与Unity进行RPG游戏,并且在执行任务系统时,特别是杀死一定数量的敌人时,我发现在场景中有3个敌人,并成为任务的目标:杀死3个敌人。如果我在激活任务之前杀死他们,并且后来激活任务不会给我奖励(在这种情况下是经验)。我怎么能告诉敌人,如果任务发现我已经杀死了必要的敌人以获得任务,同样给我奖励呢?如何在场景中实现“杀死”事件的意识
这里两个需要的脚本,我认为:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuestObject : MonoBehaviour {
public int questNumber;
public QuestManager qManager;
public string startText;
public string endText;
public bool isItemQuest;
public string targetItem;
public bool isEnemyQuest;
public string targetEnemy;
public int enemiesToKill;
private int enemyKillCount;
private PlayerStats playerStats;
public int EXPToGive;
void Start() {
playerStats = FindObjectOfType <PlayerStats>();
}
void Update() {
if (isItemQuest) {
if (qManager.itemCollected == targetItem) {
qManager.itemCollected = null;
EndQuest();
}
}
if (isEnemyQuest) {
if (qManager.enemyKilled == targetEnemy) {
qManager.enemyKilled = null;
enemyKillCount++;
}
if (enemyKillCount >= enemiesToKill) {
EndQuest();
}
}
}
public void StartQuest(){
qManager.ShowQuestText (startText);
}
public void EndQuest(){
qManager.ShowQuestText (endText);
playerStats.AddEXP (EXPToGive);
qManager.questCompleted [questNumber] = true;
gameObject.SetActive (false);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour {
public int startingHealth;
public int currentHealth;
public GameObject damageBurst;
private PlayerStats playerStats;
public int EXPToGive;
public string enemyQuestName;
private QuestManager qManager;
void Start()
{
// Setting up the references.
//anim = GetComponent <Animator>();
//enemyAudio = GetComponent <AudioSource>();
//enemyMovement = GetComponent <EnemyMovement>();
//enemyAttacking = GetComponentInChildren <EnemyAttack>();
// Set the initial health of the player.
currentHealth = startingHealth;
playerStats = FindObjectOfType <PlayerStats>();
qManager = FindObjectOfType <QuestManager>();
}
void Update()
{
if (currentHealth <= 0) {
qManager.enemyKilled = enemyQuestName;
Destroy (gameObject);
playerStats.AddEXP (EXPToGive);
}
}
public void TakeDamage (int amountDamage)
{
// Reduce the current health by the damage amount.
currentHealth -= amountDamage;
Instantiate (damageBurst, transform.position, transform.rotation);
}
public void SetMaxHelth() {
currentHealth = startingHealth;
}
}
好的谢谢,但我不明白AddKill(this)在哪里发送这些信息,我在哪里添加AddKill。和GetKillCount()?而且我不知道如何去做敌人台你能帮我吗?我有两个分离类型的敌人哥布林和史莱姆。 – adrinator8