移动物体不会因碰撞而停止 Unity

Moving Object Not Stopping On Collision Unity

我正在使用 Unity2D 开发一款无限跳塔游戏,目前正在研究一个不断移动的物体,如果发生接触,该物体会导致玩家死亡。如果玩家从平台上掉下来或离开屏幕,他们也可能死亡。所有死亡方法都依赖于我用作 Killbox 的 BoxCollider2D(相应标记) - 玩家精灵有一个 RigidBody2D 和 BoxCollider2D 连接到它 - 所以有一个连接到主相机(它随着玩家精灵通过level) 和移动对象的顶部。

我的当前代码可以在玩家死亡时出现游戏结束屏幕,但对象继续移动而其他一切都停止。

这是我的移动对象代码:

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() 中作为游戏对象被调用。添加后,水对象会在碰撞时停止。

只想说声谢谢@D.B 你的帮助和对我的包容 - 如果我提供的信息不是你能够帮助我所需的一切,我深表歉意

您在 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”(第一个字母大写?)。你也应该在角色上有一个 rigidbody2d。

错误来自您代码的另一部分或 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
}