在拿起 4 個物件后,我試圖讓我的游戲物件角色在視頻游戲中從一個位置移動到另一個位置。它會傳送,但一旦傳送,它就會繼續傳送到指定位置。我不知道如何阻止它,所以它讓我可以自由地移動到新位置。使它轉到新位置的是if (scoreValue >= 4){winTextObject.SetActive(true);}。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerScript: MonoBehaviour {
private Rigidbody2D rd2d;
public float speed;
public Text scoreText;
private int scoreValue;
public GameObject winTextObject;
public GameObject loseTextObject;
private int lives;
public Text livesText;
// Start is called before the first frame update
void Start() {
rd2d = GetComponent < Rigidbody2D > ();
scoreValue = 0;
scoreText.text = "Score: " scoreValue.ToString();
winTextObject.SetActive(false);
loseTextObject.SetActive(false);
lives = 3;
}
// Update is called once per frame
void FixedUpdate() {
float hozMovement = Input.GetAxis("Horizontal");
float verMovement = Input.GetAxis("Vertical");
rd2d.AddForce(new Vector2(hozMovement * speed, verMovement * speed));
}
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.collider.tag == "Coin") {
scoreValue = 1;
scoreText.text = "Score: " scoreValue.ToString();
Destroy(collision.collider.gameObject);
} else if (collision.collider.tag == "Enemy") {
lives -= 1;
livesText.text = lives.ToString();
Destroy(collision.collider.gameObject);
}
}
void Update() {
if (scoreValue >= 4) {
winTextObject.SetActive(true);
} {
livesText.text = "Lives: " lives.ToString();
}
if (lives <= 0) {
loseTextObject.SetActive(true);
}
}
private void OnCollisionStay2D(Collision2D collision) {
if (collision.collider.tag == "Ground") {
if (Input.GetKey(KeyCode.W)) {
rd2d.AddForce(new Vector2(0, 3), ForceMode2D.Impulse);
}
}
if (Input.GetKey("escape")) {
Application.Quit();
}
if (scoreValue == 4) {
transform.position = new Vector2(64.0 f, -1.0 f);
}
}
}
uj5u.com熱心網友回復:
您可以添加一個額外的布爾變數,以便它只輸入if一次。
- 一開始你宣告
private bool isGameOver; - 在里面
Start()你確保isGameOver = false - 更改您的最終條件:
if (scoreValue == 4 && !isGameOver)
{
isGameOver = true;
transform.position = new Vector2(64.0f, -1.0f);
}
有了這個添加,您將只觸發一次“傳送”。
uj5u.com熱心網友回復:
我認為您應該在您希望發生運輸事件的地方放置一個標志。
bool isTransported = false;
然后在 Update() 中進行比較
if (scoreValue >= 4) isTransported == true;
if (isTransported) transform.position = new Vector2(64.0 f, -1.0 f);
如果您想通過以下方式進行另一次運輸,則重置布林值:
isTranported == false;
如果你想在你的游戲中做更多的交通事件,我也建議你。您應該明確地將您的分數與一個值或一系列值進行比較。否則,您的活動將無法正常作業。例如,當得分 >=4 時,您希望您的角色傳送到位置 A,當得分 >=10 時,您的角色移動到位置 B,則這里的條件重疊。游戲將執行這兩個事件,因為 >=10 也是 >=4。
uj5u.com熱心網友回復:
通常,不是每幀輪詢檢查這些值,而是讓您撰寫事件驅動的代碼!
根本不需要額外的標志。
至于OnCollisionEnter2D實際上似乎是其中兩個值改變而做例如唯一的地方
// NOT NEEDED AT ALL
// private void Update() { }
private void OnCollisionEnter2D(Collision2D collision)
{
// In general rather use "CompareTag" instead of string "=="
// While "==" silently fails in case of typos or non-existent tags
// "CompareTag" will throw an error making your debugging easier and is also slightly faster since it uses Hashes
if (collision.gameObject.CompareTag("Coin"))
{
Destroy(collision.gameObject);
scoreValue ;
scoreText.text = $"Score: {scoreValue}";
if(scoreValue == 4)
{
winTextObject.SetActive(true);
// in general never set the position via Transform when dealing with Rigidbodis!
rd2d.position = new Vector2(64.0 f, -1.0 f);
}
}
else if (collision.gameObject.CompareTag("Enemy"))
{
Destroy(collision.gameObject);
lives--;
livesText.text = $"Lives: {lives}";
if (lives <= 0)
{
loseTextObject.SetActive(true);
}
}
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
if (Input.GetKey(KeyCode.W))
{
rd2d.AddForce(new Vector2(0, 3), ForceMode2D.Impulse);
}
}
if (Input.GetKey("escape"))
{
Application.Quit();
}
// no need to poll check the "lives" here either
}
您甚至可以更進一步地使用屬性,這樣您就可以將行為與欄位的任何更改結合起來,甚至可以從外部設定它們而無需雙重實作:
// This only stores the value (backing field) but you will never touch it directly ;)
private int _scoreValue;
// This now is a property that allows everyone to read the value
// but only this class to set it (for now)
// additionally every time you set the value it will update the display and react to the => 4 condition
public int scoreValue
{
get => _scoreValue;
private set
{
_scoreValue = value;
scoreText.text = $"Score: {_scoreValue}";
if(_scoreValue == 4)
{
winTextObject.SetActive(true);
// in general never set the position via Transform when dealing with Rigidbodis!
rd2d.position = new Vector2(64.0 f, -1.0 f);
}
}
}
// The same thing you can also make for the lives
private int _lives;
public int lives
{
get => _lives;
private set
{
_lives = value;
livesText.text = $"Lives: {_lives}";
if (_lives <= 0)
{
loseTextObject.SetActive(true);
}
}
}
所以現在你會簡單地做
private void OnCollisionEnter2D(Collision2D collision)
{
// In general rather use "CompareTag" instead of string "=="
// While "==" silently fails in case of typos or non-existent tags
// "CompareTag" will throw an error making your debugging easier and is also slightly faster since it uses Hashes
if (collision.gameObject.CompareTag("Coin"))
{
Destroy(collision.gameObject);
scoreValue ;
}
else if (collision.gameObject.CompareTag("Enemy"))
{
Destroy(collision.gameObject);
lives--;
}
}
但是以后可以添加越來越多的方式來說明如何失去或獲得分數和生命,而不必記住還要實作文本更新和條件檢查。你只能做這些曾經的屬性中;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324771.html
上一篇:無法在層碰撞矩陣中選擇玩家和敵人
