玩家触摸地板时死亡

时间:2019-05-28 21:14:03

标签: c# unity3d

我是根据YouTube教程https://www.youtube.com/playlist?list=PLPV2KyIb3jR5QFsefuO2RlAgWEz6EvVi6制作的,当时是第8集,当我开始游戏时,出现了最后的游戏文字,我认为这是因为玩家触地,所以看不到两者之间的任何区别我制作的代码和YouTuber

游戏管理员:     使用UnityEngine;

public class GameManager : MonoBehaviour
{
bool gameHasEnded = false;
// Start is called before the first frame update
public void EndGame ()
{
    if (gameHasEnded == false)
    {
        gameHasEnded = true;
        Debug.Log("GAME OVER");

    }


}
}

玩家冲突:     使用UnityEngine;

public class PlayerCollision : MonoBehaviour
{

public PlayerMovement movement;

void OnCollisionEnter (Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Block")
    {
        movement.enabled = false;
    }   FindObjectOfType<GameManager>().EndGame();
}
}

玩家移动:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{   //to substitute rigidbody to rb
public Rigidbody rb;

public float forwardForce = 2000f;

public float sidewaysForce = 500f;

// Update is called once per frame
void FixedUpdate()
{
    //add forward force
    rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    //only exectued if condition is met
    if ( Input.GetKey("d") )
    {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, 
ForceMode.VelocityChange);


    }

    if (Input.GetKey("a"))
    {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, 
ForceMode.VelocityChange);


    }
    if (rb.position.y < -1f)
    {
       FindObjectOfType<GameManager>().EndGame();
    }

}
}

1 个答案:

答案 0 :(得分:3)

问题在这里:

void OnCollisionEnter (Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Block")
    {
        movement.enabled = false;
    }   FindObjectOfType<GameManager>().EndGame();// <--- It is outside the if
}

应该是这样的:

void OnCollisionEnter (Collision collisionInfo)
{
    if (collisionInfo.collider.tag == "Block")
    {
        movement.enabled = false;
        FindObjectOfType<GameManager>().EndGame();
    }
}

只要您碰到某物,它就会结束游戏,而不是仅在一个障碍物时结束游戏。