Unity 3DCheck if int is met and do something?
Unity 3DCheck if int is met and do something?
在我的游戏中,玩家可以拾取树叶、石头和原木。我想给玩家添加条件,当某个拾取等于 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 根木头组成的触发器。
我希望有人能帮助我设置它,因为我作为设计师很难编写脚本。
如果您充分理解您的需求,这是您可以使用的简单示例。
您可以将委托与属性结合使用,以便在设置变量值时使事情发生。
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;
}
}
}
我累了,所以我可能犯了一个错误。另外,如有不明白您的需求,请见谅!
在我的游戏中,玩家可以拾取树叶、石头和原木。我想给玩家添加条件,当某个拾取等于 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 根木头组成的触发器。
我希望有人能帮助我设置它,因为我作为设计师很难编写脚本。
如果您充分理解您的需求,这是您可以使用的简单示例。 您可以将委托与属性结合使用,以便在设置变量值时使事情发生。
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;
}
}
}
我累了,所以我可能犯了一个错误。另外,如有不明白您的需求,请见谅!