Unity3d 我应该使用哪个对撞机,为什么要使用它?

Unity3d 我应该使用哪个对撞机,为什么要使用它?,unity3d,scripting,Unity3d,Scripting,我的想法基本上是当我得分时,我得到2分,但当我触摸某个对撞机时,它变为3分,我很难弄清楚该使用什么对撞机以及如何使用它们 我知道我需要使用另一个ontriggerenter 当我触摸立方体时,它应该变为3 首先,我们需要创建一个GameManager来处理Bool,该Bool检查我们当前是否在线。我们这样做是为了从所有脚本中访问它 此代码应位于GameManager对象中 // Variable to check if the player is on the line or not publ

我的想法基本上是当我得分时,我得到2分,但当我触摸某个对撞机时,它变为3分,我很难弄清楚该使用什么对撞机以及如何使用它们

我知道我需要使用另一个ontriggerenter

当我触摸立方体时,它应该变为3


首先,我们需要创建一个GameManager来处理Bool,该Bool检查我们当前是否在线。我们这样做是为了从所有脚本中访问它

此代码应位于GameManager对象中

// Variable to check if the player is on the line or not
public bool stayingOnLine = false;

#region Singelton
public static GameManager instance;

void Awake()
{
    if (instance != null) {
        Debug.LogWarning("More than one Instance of GameManager found");
        return;
    }

    instance = this;
}
#endregion
然后我们将此代码添加到处理LineCollider的游戏对象中,以处理玩家何时进入线和何时离开线。当这种情况发生时,我们从GameManager更改变量

此代码应该位于设置为IsTrigger的LineCollider所在的游戏对象中

GameManager gm;

void Start() {
        gm = GameManager.instance;
}
    
void OnTriggerEnter(Collider col) {
    // Player has entered the Line ColliderBox
    if (col.CompareTag("Player Tag"))
        gm.stayingOnLine = true;
}

void OnTriggerExit(Collider col) {
    // Player has left the Line ColliderBox
    if (col.CompareTag("Player Tag"))
        gm.stayingOnLine = false;
}
之后,我们还需要向管理HoopCollider的GameObject添加代码。因为当球进入时,我们需要检查stayingOnline是真是假,然后给出不同的分数

GameManager gm;

void Start() {
        gm = GameManager.instance;
}
    
void OnTriggerEnter(Collider col) {
    // Ball has entered the Hoop ColliderBox
    if (!col.CompareTag("Ball Tag"))
        return;
    
    if (gm.stayingOnLine)
       ScoringSystem.theScore += 3;
    else
       ScoringSystem.theScore += 2;
}

评论不用于扩展讨论;这段对话已经结束。
GameManager gm;

void Start() {
        gm = GameManager.instance;
}
    
void OnTriggerEnter(Collider col) {
    // Ball has entered the Hoop ColliderBox
    if (!col.CompareTag("Ball Tag"))
        return;
    
    if (gm.stayingOnLine)
       ScoringSystem.theScore += 3;
    else
       ScoringSystem.theScore += 2;
}