我最近刚刚问了另一个问题,我很快就得到了答复,所以我希望与此问题一样。无论如何,我正在制作2D平台游戏(类似超级肉男孩),并且添加了此炫酷的“ Dash”功能,当您按下Shift时会触发该功能。它将添加一个x轴力和一些y轴力,以使其具有类似跳跃的感觉。但是,AddForce函数在游戏中是如此不一致。有时,y轴力大于x轴,有时x轴增加一半(Δε)。我能想到发生这种情况的唯一原因是因为我增加了重力以实现适当的不现实但好看的跳跃,并且在玩家冲刺时略有增加。还是当我在跳跃的高峰期冲刺时,与冲刺开始时相比有更大的力量?我很困惑。
这里是update()函数中的所有内容。
void Update()
{
//Flipping and moving lightfeet
if (Input.GetKey(KeyCode.A))
{
anim.SetFloat("Horizontal", 1);
x = -1;
scale.x = -4;
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else if (Input.GetKey(KeyCode.D))
{
anim.SetFloat("Horizontal", 1);
x = 1;
scale.x = 4;
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else //no keys pressed
{
anim.SetFloat("Horizontal", 0);
rb.velocity = new Vector2(0f, rb.velocity.y);
anim.SetBool("Sprint", false); //even after sprinting and then stopping before to jump, sprint anim wont play
}
transform.localScale = scale;
//Accumulating jump charge and increasing speed!
if (Input.GetKey(KeyCode.Space) && rb.velocity.y == 0f) //only if space is held down and there is no vertical movement
{
jumpAccumulator += Time.deltaTime * 25f; //accumulates jump power over time
if (jumpAccumulator >= maxJumpAccumulator) //maximum jump power
{
jumpAccumulator = maxJumpAccumulator;
}
speed *= sprintSpeed; //increase speed when charging up
if(speed >= maxSpeed) //maximum speed
{
speed = maxSpeed;
}
}
//Ground detection
groundHit = Physics2D.BoxCast(bcollider.bounds.center, bcollider.bounds.size, 0f, Vector2.down, 0.1f ,groundmask);
mapHit = Physics2D.BoxCast(bcollider.bounds.center, bcollider.bounds.size, 0f, Vector2.down, 0.1f, mapMask);
if (groundHit.collider != null || mapHit.collider != null) //When you on the ground!
{
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", false);
anim.SetBool("Fall", false);
canJump = true;
canDoubleJump = true;
canDash = false;
rb.gravityScale = 15; //nomral gravity scale
//animations for running or sprinting
if (speed <= initialspeed)
{
anim.SetBool("Sprint", false);
}
else if (speed > initialspeed && rb.velocity.x > initialspeed && rb.velocity.y == 0 || speed > initialspeed && rb.velocity.x < -initialspeed)
{
anim.SetBool("Sprint", true);
}
}
else if (groundHit.collider == null || mapHit.collider == null) //if LF is not on ground!
{
canJump = false;
anim.SetFloat("Horizontal", 0f);
}
//falling without jumping
if (rb.velocity.y < 0 && anim.GetBool("Jumped") == false && anim.GetBool("DoubleJumped") == false && anim.GetBool("Dashed") == false)
{
anim.SetBool("Fall", true);
anim.SetBool("Sprint", false);
canDash = true;
}
JumpDash();
}
void JumpDash()
{
//Jump
if (Input.GetKeyUp(KeyCode.Space) && canJump == true)
{
rb.velocity = Vector2.up * (jumpForce + jumpAccumulator);
anim.SetBool("Jumped", true);
anim.SetBool("DoubleJumped", false); //setting false to prevent bugs
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
canDash = true;
jumpAccumulator = 1;
}
//Double Jump
if(Input.GetKeyDown(KeyCode.W) && canJump == false && canDoubleJump == true)
{
rb.velocity = Vector2.up * jumpForce;
canDoubleJump = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", true);
anim.SetBool("Dashed", false); //disabling dash animation so it doesn't play when dashing then double jumping (because conditions for both can be true at the same time)
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
anim.SetBool("Fall", false);
doubleJumpFX.Play();
DashFX.Stop();
}
//Dash
if(Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
rb.AddForce(new Vector2(dashForce * x, dashUpForce), ForceMode2D.Impulse);
rb.gravityScale += dashGravity; //increasing gravity for dashing
canDash = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", true);
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
doubleJumpFX.Stop();
DashFX.Play();
}
}
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collider: " + collision.collider.name);
if (collision.collider.name == "platform" || collision.collider.name == "mapColliders")
{
speed = initialspeed; //set speed to normal on hitting the ground (difference between this and raycast is this returns true for only one frame)
anim.SetBool("Dashed", false); //setting false to this anim here again to prevent buggy animations
anim.SetBool("Fall", false);
doubleJumpFX.Stop();
DashFX.Stop();
}
if (collision.collider.name == "Respawn area")
{
transform.position = new Vector2(-13f, 4f);
}
}
AddForce的不稳定性是因为您要添加玩家现有的力量。您是否曾在致电AddForce之前试图消灭现有部队?只需添加以下行
rb.velocity = new Vector2 (0, 0);
在AddForce函数前面。您的破折号功能应如下所示
//Dash
if(Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
rb.velocity = new Vector2 (0, 0);
rb.AddForce(new Vector2(dashForce * x, dashUpForce), ForceMode2D.Impulse);
rb.gravityScale += dashGravity; //increasing gravity for dashing
canDash = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", true);
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
doubleJumpFX.Stop();
DashFX.Play();
}