0
我正在建立一個統一的android遊戲5.3.5,當挖掘scree時,玩家向上移動並不得不繼續點擊,但是當遊戲開始時玩家跌倒並離開遊戲區!我想要做的是讓玩家在遊戲開始時保持靜止,所以當屏幕被點擊時遊戲開始!這裏是代碼..停止玩家在遊戲開始時下降,並使用水龍頭開始unity2D
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;
}
}
}
添加bool; 'isFirstTouch'。讓它成爲開始的假。在更新中檢查它。觸摸時執行。說實話。如果這是真的,那麼運行你的代碼? – Thalthanas
會嘗試,使isFirstTouch false在你的開始方法是什麼意思?然後將代碼包裝在另一個if語句的更新中? –
是的。您可以使用bool創建一個新的if語句,將當前的if語句塊更新。你也在使用物理。您應該像@ zayedUpal建議的那樣將這個bool的'isKinematic'值更改。 – Thalthanas