【发布时间】:2017-09-05 18:44:55
【问题描述】:
我正在统一 5.3.5 中为 android 构建游戏,当点击碎石时,玩家会向上移动并且必须继续点击,但是当游戏开始时,玩家会摔倒并离开游戏区域!我想要做的是在游戏开始时让玩家静止,这样当点击屏幕时游戏就开始了!这是代码..
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public static int score = 0;
public Text scoreText;
public GameObject obsticle;
public GameObject colorChanger;
void Start () {
setRandomColor ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.velocity = Vector2.up * jumpForce;
}
scoreText.text = score.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
【问题讨论】:
-
添加一个布尔值;
isFirstTouch。一开始就让它是假的。在更新中检查它。进行触摸时。让它成为现实。如果是真的那么运行你的代码? -
会试试,你的意思是在 start 方法中使 isFirstTouch 为 false 吗?然后将更新中的代码包装在另一个 if 语句中?
-
是的。您可以使用 bool 创建一个新的 if 语句,以将当前的 if 块包装在更新中。你也在使用物理学。您应该按照@ZayedUpal 的建议更改
isKinematic的值,就像这个布尔值一样。 -
player 好像还是掉了,我做了 bool 变量 isFirstTouch,然后在 start 方法中设置为 false,同时设置 circle.isKinematic = true;正如@ZayedUpal 提到的那样,将更新中的代码包装在一个 if 语句中,该语句表示 if isFirstTouch = true;
-
在开始游戏前在编辑器中设置 isKinematic = true 会发生什么?开始游戏的时候还掉吗?