if (Input.GetKeyDown(KeyCode.Space))
{
if (jumpcheck = false)
{
bool jumpcheck = true;
gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
yield return new WaitForSeconds (2);
bool jumpcheck = false;
}
bool 是名為“jumpcheck”的變數 應該發生的是,如果按下空格鍵,則檢查變數 jumpcheck 是否為 false,如果為 false,則將 jumpcheck 設定為 true,執行跳轉命令,然后設定 jumpcheck 2 秒后變回 false。
這是所有的代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jeff : MonoBehaviour
{
public float movespeed = 5f;
public float jumppower = 10f;
// Start is called before the first frame update
void Start()
{
bool jumpcheck = false;
}
// Update is called once per frame
void Update()
{
StartCoroutine(Test ());
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position = movement * Time.deltaTime * movespeed;
}
IEnumerator Test ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (jumpcheck = false)
{
bool jumpcheck = true;
gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
yield return new WaitForSeconds (2);
bool jumpcheck = false;
}
}
}
}
我收到多個錯誤
Assets/jeff.cs(34,15): error CS1525: Invalid expression term 'bool'
Assets/jeff.cs(34,20): error CS1026: ) expected
Assets/jeff.cs(34,37): error CS1002: ; expected
Assets/jeff.cs(34,37): error CS1513: } expected
uj5u.com熱心網友回復:
你應該讓你的代碼更像這樣:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jeff : MonoBehaviour
{
public float movespeed = 5f;
public float jumppower = 10f;
bool jumpcheck;
// Start is called before the first frame update
void Start()
{
jumpcheck = false;
}
// Update is called once per frame
void Update()
{
StartCoroutine(Test ());
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position = movement * Time.deltaTime * movespeed;
}
IEnumerator Test ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (jumpcheck == false)
{
jumpcheck = true;
gameObject.GetComponent<Rigidbody2D>().AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
yield return new WaitForSeconds (2);
jumpcheck = false;
}
}
}
}
uj5u.com熱心網友回復:
首先,根據您的實作,這是沒有用的,因為它是本地的 Start 方法。
void Start()
{
bool jumpcheck = false;
}
和編譯錯誤是由于這一行,必須有==沒有=在if狀態
if (jumpcheck = false) //wrong
uj5u.com熱心網友回復:
你好,恭喜你在 StackOverflow 上提出第一個問題。我認為您的游戲代碼邏輯需要一些改進,我還編輯了您的代碼,以免出現任何編譯器錯誤
jumpcheckbool每次在同一范圍內使用變數時,您都在宣告變數(這是錯誤),您需要的是在類級別上進行一次宣告,并使用值進行宣告時間分配- 在 C# 中,當相互比較兩個值時,我們
==為此使用運算子,以及=用于賦值(有關比較運算子的更多資訊,請參閱此處 - 我還修改了您的代碼,以便您的邏輯應該相同但更可能沒有錯誤,協程與更新不一致,如果您在更新中的每一幀都運行協程,您最終可能會遇到性能問題。所以我重寫了代碼,讓 Coroutine 只處理計時,所以在最壞的情況下它只會在 2 秒后被呼叫
- 最后,我謙虛地要求您遵循通用的 C# 命名約定,我將一些變數名和類名從 lowerCase jeff 修改為PasCalCase Jeff。 所以這里是 C# 命名約定
最后這里是應用了所有提到的代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Jeff : MonoBehaviour
{
public float movespeed = 5f;
public float jumppower = 10f;
private bool jumpCheck = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !jumpCheck)
{
gameObject.GetComponent<Rigidbody2D>()
.AddForce(Vector3.up * jumppower, ForceMode2D.Impulse);
jumpCheck = true;
StartCoroutine(ResetJumpCheck());
}
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position = movement * Time.deltaTime * movespeed;
}
IEnumerator ResetJumpCheck()
{
yield return new WaitForSeconds(2);
jumpCheck = false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316219.html
上一篇:InvalidOperationException:無法從復合“UnityEngine.InputSystem.Compposites.Vector2Composite”中讀取“Vector2”型別的
