在我的游戏中,玩家可以拾取树叶,石头和木制木头。我想为玩家添加条件,当某个拾取等于5时,该条件将被激活。
播放器由5个模块组成,当拾取某些物品时,每个模块将被拾取器取代。这意味着玩家可以用5片叶子,5块岩石或5块木头或者这些物品的混合物组成。
[Header("Real-Time Statistics")]
public int numberOfLeaves;
public int numberOfLogs;
public int numberOfRocks;
此方面显示在检查器中,并在玩家找到取件时更新。
void OnTriggerStay2D(Collider2D other){
if(Input.GetKeyDown(KeyCode.P)){
if(other.gameObject.tag == "Leaf"){
Pickup(1); // 1 = leaf, according to the ModuleStateAndTextureController script.
numberOfLeaves += 1;
Destroy(other.gameObject); // destroy the pickup item as we are picking it up
}
if(other.gameObject.tag == "WoodLog"){
Pickup(2); // 2 = wood log, according to the ModuleStateAndTextureController script.
numberOfLogs += 1;
Destroy(other.gameObject); // destroy the pickup item as we are picking it up
}
if(other.gameObject.tag == "Rock"){
Pickup(3); // 3 = rock, according to the ModuleStateAndTextureController script.
numberOfRocks += 1;
Destroy(other.gameObject); // destroy the pickup item as we are picking it up
}
}
}
当找到某个拾取时,脚本的这一部分会向int添加一个数字。当玩家放下皮卡时,我在脚本中有类似的部分。
我如何编写一个脚本来检查玩家是否符合某些条件,即。如果玩家由5片叶子组成,他可以跳得更高并且下降更慢?
我想到的是:如果玩家包含5片叶子jumpPower = 2000;
或类似的东西。这将是我猜的玩家对象中添加的特征,但我还需要知道如何在其他对象上使用这些int,即可以检查玩家是否包含3片叶子和2根木制日志的触发器。
我希望有人可以帮助我解决这个问题,因为我作为设计师的脚本很难。
答案 0 :(得分:1)
如果了解您的需要,这是您可以使用的简单示例。 您可以使用委托与属性相结合,在设置变量值时使事情发生。
public Class MyClass : MonoBehaviour {
// Delegates, like a pointer in C, but to method(s) instead of variable
public delegate void valueLogsChangedDelegate (int valueLogs);
public valueLogsChanged valueLogsChanged = delegate { };
private int _numberOfLogs;
// Property, when you set numberOfLogs (eg numberOfLogs = 10), every thing in "set" is executed
public int numberOfLogs {
get {
return _numberOflogs;
}
set {
_numberOfLogs = value;
valueLogsChanged(_numberOflogs);
}
}
/// <summary>
/// Awake is called when the script instance is being loaded.
/// </summary>
void Awake()
{
// Subscribe to the delegate, you can add as many methods as you want. Every methods that subscribe to the delegate will be excuted when the delegate is called
valueLogsChanged += Method;
}
void Method(int valueLogs)
{
if (valueLogs > 5)
{
jumpPower = 2000;
}
}
}
我累了所以我可能犯了一个错误。 Morover,如果我不明白你的需要,请原谅!