我想讓玩家雙跳,但玩家可以無限跳躍,任何人都可以幫助我嗎?我使用統一5讓玩家雙跳
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
private bool grounded;
private Rigidbody2D rb;
private int jumpCount = 0;
private int maxJumps = 2;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown (KeyCode.Space) && jumpCount < maxJumps) {
rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2 (rb.velocity.x, jumpHeight);
jumpCount = jumpCount + 1;
}
if (grounded == true) {
jumpCount = 0;
}
if (Input.GetKey (KeyCode.D)) {
rb.velocity = new Vector2 (moveSpeed,rb.velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
rb.velocity = new Vector2 (-moveSpeed,rb.velocity.y);
}
}
void OnCollisionEnter2D (Collision2D collider){
if (collider.gameObject.tag == "Ground") {
grounded = true;
}
}
}
什麼是不工作,是你的角色只跳一次,他從不跳,你得到一個錯誤...並將你的代碼從'Update'移動到'FixedUpdate'。 – CNuts
他只跳一次 – MightyElf
哪段代碼應該移動到fixedupdate? – MightyElf