簡體   English   中英

移動對象在碰撞 Unity 時不會停止

[英]Moving Object Not Stopping On Collision Unity

我正在使用 Unity2D 開發一個無限跳塔游戲,目前正在研究一個不斷移動的物體,如果發生接觸,它會導致玩家死亡。 如果玩家從平台上掉下來或離開屏幕,他們也會死亡。 所有的死亡方法都依賴於我用作 Killbox 的 BoxCollider2D(相應地標記) - 玩家精靈附有一個 RigidBody2D 和 BoxCollider2D - 所以有一個連接到主相機(它隨着玩家精靈通過水平)和移動物體的頂部。

我目前的代碼一直工作到玩家死亡時游戲結束屏幕出現的點,但是對象繼續移動而其他一切都停止了。

這是我的移動對象代碼:

public class Water : MonoBehaviour   {
private Collider2D playerCollider;
public ControllerNew thePlayer;

private float speed = 2f; 

public GameManager theGameManager;

//reference scoremanager
private ScoreManager theScoreManager;

bool shouldMove = true;

// Start is called before the first frame update
void Start()
{
    theScoreManager = FindObjectOfType<ScoreManager>();
    thePlayer = FindObjectOfType<ControllerNew>();
    lastPlayerPosition = thePlayer.transform.position;
}

// Update is called once per frame
void Update()
{
    if (shouldMove = true)
    {
        transform.position += Vector3.up * speed * Time.deltaTime;

        if (theScoreManager.scoreCount > 100 && shouldMove)
        {
            transform.position += Vector3.up * (speed+1) * Time.deltaTime;
        }

        if (theScoreManager.scoreCount > 250 && shouldMove)
        {
            transform.position += Vector3.up * (speed +2) * Time.deltaTime;
        }

        if (theScoreManager.scoreCount > 500 && shouldMove)
        {
            transform.position += Vector3.up * (speed+4) * Time.deltaTime;
        }

        if (theScoreManager.scoreCount > 1000 && theScoreManager.scoreCount > theScoreManager.hiScoreCount && shouldMove)
        {
            transform.position += Vector3.up * (speed+5) * Time.deltaTime;
        }
    }

    }

void OnTriggerEnter2D(Collider2D other)
{

    if (other.gameObject.tag == "Player New")
    {
        transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
    }

    //call object by its tag
    if (other.gameObject.tag == "Killbox")
    {
        shouldMove = false;
        if (shouldMove = false){ 
        theGameManager.RestartGame();
        transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
        }
    }
}


編輯(問題已解決)

所以事實證明,在將 Water.StopMoving() 方法添加到我的控制器腳本后,水並沒有在 void Start() 中作為游戲對象被調用。 添加后,水物體在碰撞時停止。

只想說謝謝@DB 的幫助和對我的支持 - 如果我提供的信息不是您能夠幫助我所需的一切,我深表歉意

你在Update()方法的第一行犯了一個錯誤:

if (shouldMove = true)

您將 bool 設置為 true,而不是進行比較。 使用 double =否則它將在每一幀將 bool 設置為 true。

if (shouldMove == true)

順便說一下,您可以簡化這部分:

//call object by its tag
if (other.gameObject.tag == "Killbox")
{
    shouldMove = false;
    theGameManager.RestartGame();
    transform.position += Vector3.zero * (speed * 0) * Time.deltaTime;
}

(你也忘記了=

我用這個簡化的腳本做了一個測試

void Update()
{
    if (shouldMove == true)
    {
        Debug.Log("move");
        transform.position += Vector3.up * speed * Time.deltaTime * GetDifficultyFactor();

    }

}

private float GetDifficultyFactor()
{
    float factor = 1f;
    if(theScoreManager.scoreCount > 100)
    {
        factor += 1f;
    }

    if (theScoreManager.scoreCount > 250)
    {
        factor += 2f;
    }

    // Add all your speed modification condition here
    return factor;
}

void OnTriggerEnter2D(Collider2D other)
{
    Debug.Log("trigger");
    //call object by its tag
    if (other.gameObject.tag == "Killbox")
    {
        Debug.Log("die");
        shouldMove = false;
    }
}

它工作正常。 你確定你有一個 collider2D 設置來觸發你的角色,標簽為“Killbox”(第一個字母大寫?)。 你也應該在角色上有一個剛體2d。

錯誤來自代碼的另一部分,或者 collider2D/tag/RigidBody2D 出現問題。 沒有看到一切,很難幫助你更多。

您應該嘗試添加一些Debug.Log()或使用帶斷點的 debugeur 以確保代碼進入您的“die”語句,然后不繼續Update if 語句。 如果是,則表示您可能在腳本的另一部分設置了shouldMove變量。

關於評論中討論的回答

我想你想在這兩個腳本中制作這個OnTriggerEnter2D邏輯。

在沒有看到你所有的項目的情況下,我建議你在你的角色和你的水腳本之間做一個參考。 然后當玩家死亡時,玩家腳本將調用水上的一個方法來阻止它。

public class Player : MonoBehaviour
{
    public Water Water;

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Killbox")
        {
            Debug.Log("die");
            Water.StopMoving();
        }
    }
}

public class Water : MonoBehaviour
{
    private bool shouldMove;

    public void Update()
    {
        ...
    }

    public void StopMoving()
    {
        shouldMove = false;
    }

    // No Trigger logic here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM