Привет!Мой персонаж делает неожиданный 3-й прыжок в Unity2D
В настоящее время я работаю над 2D-платформерной игрой в движке Unity, где я создал персонажа, который может забить doubleblejump.
В начале все было очень хорошо, но теперь я понял, что есть ошибка в игре, которая позволяет ему выполнить третий прыжок (по какой-то причине). И я не могу ради этого понять, в чем проблема.
PlayerController.cs
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
/*
TODO: Find out why the character ocationally
gets 3 jumps instead of 2.
I think it's the "isGrounded" that returns
a false posetive.
*/
[Header("Ground Recognition")]
public BoxCollider2D groundCollider;
public LayerMask groundAbles;
[Header("Audio")]
public GameObject jumpSound = null;
[Header("Visual")]
public GameObject jumpEffect = null;
float speed = 5f;
int maxJumps = 2;
int currentJumps = 0;
bool isGrounded = false;
float boundsLength = 0;
Vector3 movement;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
CheckGroundCollision();
JumpLogic();
movement = new Vector3(Input.GetAxis("Horizontal") * speed, 0, 0);
movement *= Time.deltaTime;
transform.position += movement;
CheckGroundCollision();
}
void CheckGroundCollision()
{
isGrounded = groundCollider.IsTouchingLayers(groundAbles);
}
void JumpLogic()
{
if (isGrounded)
currentJumps = 0;
if (Input.GetButtonDown("Jump") && currentJumps < maxJumps)
{
GameObject newJumpSound = (GameObject)GameObject.Instantiate(jumpSound, (Vector2)transform.position, transform.rotation);
GameObject newJumpEffect = (GameObject)GameObject.Instantiate(jumpEffect, (Vector2)transform.position - new Vector2(0, 0.25f), transform.rotation);
GameObject.Destroy(newJumpEffect, 0.2f);
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
currentJumps++;
CheckGroundCollision();
}
}
}
Вся помощь ценится!
как тест, попробуйте установить maxjumps на 1. и посмотреть, сможете ли вы получить 2 прыжка. это может быть порядок операций, и вы получаете isGrounded = true после нажатия прыжка, но перед тем, как покинуть контакт с землей. –
Можете ли вы отлаживать или печатать какую-либо информацию во время ее работы? Мне было бы любопытно, если 'isGrounded' вызывает какие-либо проблемы и преждевременно перезагружает' currentJumps'. – Ian
Я протестировал теорию, и проблема действительно основана на том, что currentJumps преждевременно перезагружается! – VonRiddarn