遇到障碍物时如何停止游戏
How to stop the game when the obstacle is hit
我正在统一开发一款游戏,因为我能够 运行 游戏,但是当游戏遇到障碍时,游戏不会 stop.I 使用一种方法来停止游戏但是没用。
我的脚本是
Player.cs
public class Player : MonoBehaviour{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
public Rigidbody2D myRigidbody2D;
void Start()
{
this.myRigidbody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp("space"))
{
this.myRigidbody2D.velocity = Vector2.zero;
this.myRigidbody2D.AddForce(jumpForce);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
public void Die()
{
if (Input.GetKeyUp ("r"))
{
Application.LoadLevel (Application.loadedLevel);
}
}
}
Generate.cs
using UnityEngine;
public class Generate : MonoBehaviour
{
public GameObject Pencils;
int score = 0;
public Player p;
void Start()
{
InvokeRepeating("CreateObstacle", 2.8f, 2.5f);
}
void OnGUI ()
{
GUI.color = Color.black;
GUILayout.Label(" Score: " + score.ToString());
}
public void CreateObstacle()
{
Instantiate(Pencils);
{
score++;
}
//Player player = Pencils.GetComponent<Player> ();
//player.Die();
}
public void destroy()
{
p.Die ();
//Generate.SetTrigger ("Die");
//Die = true;
//public bool isChecked = false;
//public void Check(Die)
p mygen=destroy.GetComponent<p>();
if (mygen)
{
if (p.transform.tag == "Die")
mygen.isChecked = true;
}//else
// is Checked=false;
{
CancelInvoke("CreateObstacle");
}
}
}
无法在列表中找到您的 'game stopping' 代码。如果 'stopping the game' 你的意思是暂停它或停止游戏中的时间流,那么 Time.timeScale 应该可以。
要暂停游戏,您可以执行 Time.timeScale = 0f;
,要恢复游戏,您只需执行 Time.timeScale = 1f;
如果您使用的是 Unity 的物理引擎,您可能还需要修改 Time.fixedDeltaTime(这样您的物理在游戏暂停时不会 运行)。
I want execute the void destroy() method from the Generate.cs as the Die() method executes from the Player.cs
如果您想在执行 player.cs 中的 die() 时从 generate.cs 调用 destroy(),您只需在 die() 中调用 destroy()。
您需要在 player.cs 中创建一个游戏对象,它将成为您的 generate.cs:
的一个实例
public gameObject generateInstance;
然后您需要在 player.cs 脚本的 die 方法中调用该 generateInstance 的 destroy 方法:
public void Die()
{
if (Input.GetKeyUp ("r"))
{
generateInstance.destroy();
Application.LoadLevel (Application.loadedLevel);
}
}
不要忘记将 generateInstance 设置为 Generate.cs,方法是将其拖到 Unity 检查器中的 Player.cs 脚本中,或者直接通过代码获取它。
我正在统一开发一款游戏,因为我能够 运行 游戏,但是当游戏遇到障碍时,游戏不会 stop.I 使用一种方法来停止游戏但是没用。
我的脚本是
Player.cs
public class Player : MonoBehaviour{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 300);
public Rigidbody2D myRigidbody2D;
void Start()
{
this.myRigidbody2D = this.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp("space"))
{
this.myRigidbody2D.velocity = Vector2.zero;
this.myRigidbody2D.AddForce(jumpForce);
}
Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
if (screenPosition.y > Screen.height || screenPosition.y < 0)
{
Die();
}
}
public void Die()
{
if (Input.GetKeyUp ("r"))
{
Application.LoadLevel (Application.loadedLevel);
}
}
}
Generate.cs
using UnityEngine;
public class Generate : MonoBehaviour
{
public GameObject Pencils;
int score = 0;
public Player p;
void Start()
{
InvokeRepeating("CreateObstacle", 2.8f, 2.5f);
}
void OnGUI ()
{
GUI.color = Color.black;
GUILayout.Label(" Score: " + score.ToString());
}
public void CreateObstacle()
{
Instantiate(Pencils);
{
score++;
}
//Player player = Pencils.GetComponent<Player> ();
//player.Die();
}
public void destroy()
{
p.Die ();
//Generate.SetTrigger ("Die");
//Die = true;
//public bool isChecked = false;
//public void Check(Die)
p mygen=destroy.GetComponent<p>();
if (mygen)
{
if (p.transform.tag == "Die")
mygen.isChecked = true;
}//else
// is Checked=false;
{
CancelInvoke("CreateObstacle");
}
}
}
无法在列表中找到您的 'game stopping' 代码。如果 'stopping the game' 你的意思是暂停它或停止游戏中的时间流,那么 Time.timeScale 应该可以。
要暂停游戏,您可以执行 Time.timeScale = 0f;
,要恢复游戏,您只需执行 Time.timeScale = 1f;
如果您使用的是 Unity 的物理引擎,您可能还需要修改 Time.fixedDeltaTime(这样您的物理在游戏暂停时不会 运行)。
I want execute the void destroy() method from the Generate.cs as the Die() method executes from the Player.cs
如果您想在执行 player.cs 中的 die() 时从 generate.cs 调用 destroy(),您只需在 die() 中调用 destroy()。
您需要在 player.cs 中创建一个游戏对象,它将成为您的 generate.cs:
的一个实例public gameObject generateInstance;
然后您需要在 player.cs 脚本的 die 方法中调用该 generateInstance 的 destroy 方法:
public void Die()
{
if (Input.GetKeyUp ("r"))
{
generateInstance.destroy();
Application.LoadLevel (Application.loadedLevel);
}
}
不要忘记将 generateInstance 设置为 Generate.cs,方法是将其拖到 Unity 检查器中的 Player.cs 脚本中,或者直接通过代码获取它。