C# 检查int是否满足要求并采取措施?
在我的游戏中,玩家可以捡拾树叶、石头和木头。我想为玩家添加条件,当某个拾取值等于5时激活 该播放器由5个模块组成,每个模块在拾取某物时将被一个拾音器替换。这意味着玩家可以由5片树叶、5块石头或5根木头或这些物品的组合组成C# 检查int是否满足要求并采取措施?,c#,unity3d,unity5,C#,Unity3d,Unity5,在我的游戏中,玩家可以捡拾树叶、石头和木头。我想为玩家添加条件,当某个拾取值等于5时激活 该播放器由5个模块组成,每个模块在拾取某物时将被一个拾音器替换。这意味着玩家可以由5片树叶、5块石头或5根木头或这些物品的组合组成 [Header("Real-Time Statistics")] public int numberOfLeaves; public int numberOfLogs; public int numberOfRocks; 此特性显示在检查器中,并在玩家找到皮卡时更新 void
[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添加一个数字。我在脚本中有一个类似的部分,当玩家放下皮卡时
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
}
}
}
我该如何编写一个脚本来检查一个玩家是否满足某些条件,即如果该玩家有5片叶子,他将能够跳得更高,下降得更慢
我的想法是这样的:如果玩家由5片叶子组成jumpPower=2000代码>或类似的东西。我想这可能是玩家对象中添加的一个特性,但我还需要知道如何在其他对象上使用这些int,即可以检查玩家是否由3片树叶和2块木头组成的触发器
我希望有人能帮我设置这个,因为作为一名设计师,我很难编写脚本。如果你能很好地理解你的需要,这是一个简单的示例,说明你可以使用什么。
当设置变量的值时,可以将委托与属性结合使用,使事情发生
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;
}
}
}
公共类MyClass:MonoBehavior{
//委托,就像C中的指针一样,但委托给方法而不是变量
公共代表无效值日志SchangedElegate(int valueLogs);
public valueLogsChanged valueLogsChanged=委托{};
私人int_numberOfLogs;
//属性,当您设置numberOfLogs(例如numberOfLogs=10)时,“set”中的所有内容都将执行
公共整数日志{
得到{
返回_numberOflogs;
}
设置{
_numberOfLogs=值;
值日志更改(_numberOflogs);
}
}
///
///在加载脚本实例时调用Awake。
///
无效唤醒()
{
//订阅委托,您可以添加任意数量的方法。在调用委托时,将执行订阅委托的每个方法
valueLogsChanged+=方法;
}
void方法(int-valueLogs)
{
如果(valueLogs>5)
{
跳线功率=2000;
}
}
}
我累了,所以我可能弄错了。莫洛弗,如果我不明白你的需要,请原谅 您可以添加一个在资源值更改时调用的委托。代理将更改jumpPower的值。类似的内容是什么?我没有那么丰富的编码经验。我目前正在做这个:void OnTriggerEnter2D(collizer2d col){if(col.gameObject.tag==“Player”+numberOfRocks>=5){test.SetActive(true);}}}
但无法让它工作:(我会在接下来的几个小时里给你更多的细节,现在就开始工作,或者有人会在我之前回答你。我会非常感激:)!