我刚开始学习如何做一个简单的2D平台游戏的游戏在团结,我看过Blackthornprod 2D Platformer movement video学习如何做一个角色移动和跳跃。到目前为止,我能够理解大部分的视频,但是,我也遇到了一些问题与跳跃的一部分。
现在,只要我在他的视频了解,做一个人物跳跃,我需要检测字符是否接触地面或不是。要做到这一点,我需要创造条件,玩家的脚有点圆,看它是否重叠在地上,like this
所以,这里是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float JumpForce; // jumping force
private bool isGrounded; //if the player is touching the ground
public Transform GroundCheck; //object that contains the position of the
//"circle"
public LayerMask GroundLayer; //the layer that the "circle" collide with
public float radius; //radius of the circle
public float BaseNumOfJumps; //maximum number of jumps
private float NumOfJumps; //current number of jumps
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
NumOfJumps = BaseNumOfJumps;
}
void FixedUpdate()
{
//check the collision between the "circle" and the ground
isGrounded = Physics2D.OverlapCircle(GroundCheck.position,
radius,GroundLayer);
//here is the horizontal movement update part, but they are
//irrelevant so I cut them off
}
void Update()
{
//if the player touches the ground, reset the number of jumps
if (isGrounded)
{
NumOfJumps = BaseNumOfJumps;
}
//when jumping, decrease the number of jumps
if (Input.GetKeyDown(KeyCode.UpArrow) && NumOfJumps > 0)
{
rb.velocity = Vector2.up * JumpForce;
NumOfJumps--;
}
}
}
这就是问题的开始,跳跃的实际最大数总是比我中输入的值。因此,例如,如果我把BaseNumOfJumps = 2,那么我可以跳3倍而不是2,同样地,如果BaseNumOfJumps = 1,则我可以跳2倍等。
过了一会儿,我发现因为圆的大小始终是一个稍微有点比实际的球员的脚大,当我跳的是介于两者之间,球员的脚为int空气中的时刻,但圈内还是重叠与地面,其中重置跳跃的当前数量(在我的代码又名NumOfJumps),使我的性格能够做一个比BaseNumOfJumps输入的号码多跳。我不知道是真的还是假的,but here was what I imagined。 This comment under the video also described the same problem.
虽然Blackthornprod已经提到的值作为“额外的跳跃”,所以如果我在那个值,输入“1”,我会跳两次,“2”,我会跳3次,等我仍然在做的次数遇到一些困难跳跃是一致的。如果我的角色只是滑出,跳跃like this地面?圆不会与地面发生碰撞,我就能跳我BaseNumOfJumps进入跳跃的确切数字,总之:
如果我进入跳跃的最大数量为2,我将能够(再次,这只是我的猜测,所以我可能是错的):
这当然造成一些矛盾。我寻觅其他教程约跳跃和碰撞,但大多任一使用其他形式的检查碰撞(如OnCollisionStay,OnCollisionEnter)或使用我用同样的方法,但并没有深入到解释它(所以基本上他们是相同的在Blackthornprod的视频)
那么,如何解决这个问题,我怎样才能改变“圆”的半径,使其离开地面同一时刻,当他们跳的球员的脚离开?或者我应该使用其他形式的碰撞检查像我之前提到的,如果是的话,他们有什么区别,哪个是最有效的?
这是我的第一个问题在这里,我知道这是一个比较长,令人迷惑,因为我不熟悉英语,但我会很感激,如果任何人都可以提供帮助。
让我们的东西,我们希望做一个概念性的理解。我们希望玩家有相同数量的跳跃不管他们是否第一跳(使用一个跳跃)或秋季(不使用一个跳跃)。然而,一个简单的事实是肯定的:如果我们不在地面上我们停飞跳不能使用。我们可以通过保持接地跳跃的轨迹实现这一目标和空中分别跳跃。
现在,我们需要问允许使用一个接地跳的是玩家是否是地面上的唯一的问题。我们需要问允许使用奖金跳跃的唯一问题是玩家是否是在空气中和玩家是否已经离开跳跃。
所有这一切仍然是决定玩家是否已经离开跳跃。如果他们倒下了,我们就可以指望有多少跳出他们得到他们的总跳跃。如果他们跳下来,我们可以指望有多少跳出他们的跳跃减一的总数。
所有这一切都被认为给了我们以下逻辑:
int numberOfJumps = 1; //Or however many you want.
boolean initiallyJumped = false;
//Other stuff
void Update()
{
//Reset jumps BEFORE considering allowing jumps
if(isGrounded)
{
initiallyJumped = false;
resetJumpCounter();
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
if(!initiallyJumped && isGrounded)
{
jump();
initallyJumped = true;
}
else
{
int jumpsRemaining = numberOfJumps;
if(initiallyJumped)
{
jumpsRemaining--;
}
if(numberOfJumps > jumpsRemaining)
{
jump();
numberOfJumps--;
}
}
}
}
现在,如果没有使用它的初始跳只用来和玩家在地面上。奖金跳跃在任何其他情况下使用。然而,奖金跳跃的数量是由一个,如果我们最初跳下下降。