【问题标题】:Unity game troubleunity游戏麻烦
【发布时间】:2017-03-23 04:46:06
【问题描述】:

我正在 Unity 中创建一个游戏,其中我必须有一个玩家(一个球)和三个敌人(在本例中是三个旋转的圆柱体)。每当玩家击中敌人时,我需要它死(我已经这样做了)然后打印出 Game over,我不知道该怎么做。我还需要玩家在它死后重生,这是另一个我不知道该怎么做的想法。我还需要创建三个“虚拟洞”,当玩家翻过它们时,它会重生,但不会死亡。我想我可以通过创建扁平圆柱体来模拟孔,但我不知道如何让球重生,而是滚过它们。先感谢您!!请明确回答中的哪一部分。

//My player script
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody rb;
public float threshold;
public Text gameOver;

// Use this for initialization
void Start()
{
    rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
//rolls the player according to x and z values
void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.AddForce(movement * speed);
}
}




//my script that makes the enemy rotate and kills the player but after the
 player dies it just disappears
public class Rotater : MonoBehaviour {
public Text gameOver;

// Update is called once per frame
void Update ()
{
    transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);

}

//kills player
void OnCollisionEnter(Collision Col)
{
if (Col.gameObject.name == "Player")
{
    Destroy(Col.gameObject);
    gameOver.text = "Game over!";
 }

}




//my script that respawns the play if it falls off the maze
public class Respawn : MonoBehaviour
{

// respawns player if it goes below a certain point (falls of edge)
public float threshold;

void FixedUpdate()
{
    if (transform.position.y < threshold)
        transform.position = new Vector3(-20, 2, -24);
}
}

【问题讨论】:

标签: c# unity3d


【解决方案1】:

您需要创建一个 UI 来在您的游戏上绘制文本。你可以了解它here

UI 到位后,您只需使用SetActive(bool) 激活/停用相关部分。

要杀死并重生玩家,我建议您不要破坏它并重新实例化它。相反,您可以简单地停用它,然后再次使用SetActive(bool) 在新位置重新激活它。

对于孔洞,您可以使用碰撞器创建其他对象并使用OnCollisionEnter,就像您已经做的那样,但要更改玩家的位置。

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多