平滑的双跳Unity 2D平台
本文关键字:2D 平台 Unity 平滑 | 更新日期: 2023-09-27 18:16:28
我试图为Unity 2D平台游戏创造一个流畅的双跳机制但现在如果你在第一次跳跃之后跳第二次跳跃会更大而我想让它反过来如果你稍后按它,双跳应该会让你跳得更高就像大多数2D平台游戏一样这是我的代码
private void Update()
{
grounded = Physics2D.OverlapArea(transform.position, GroundedEnd.position, 1 << LayerMask.NameToLayer("Ground"));
if (grounded || !doubleJumpUsed)
{
playerRigidBody.AddForce(new Vector2(0, JumpPower));
}
if (!grounded && !doubleJumpUsed)
{
doubleJumpUsed = true;
}
UpdateJumpingAnimator();
}
private void UpdateJumpingAnimator()
{
if (grounded)
{
doubleJumpUsed = false;
}
}
试试下面的代码:
playerRigidBody.AddForce(new Vector2(0, JumpPower), ForceMode2D.Impulse);
在这种情况下,有必要减少变量JumpPower
老实说,我不确定是否有一个漂亮的方法来做你所要求的。但你试过把JumpPower和计时器连在一起吗?换句话说,角色在空中停留的时间越长,经过的时间就越长,当按下按钮时,第二次跳跃所受的力就越大。
嗨,我试图实现在一个球体(玩家)上的双跳,这是我的代码。看看是否有帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//[RequireComponent(typeof(Rigidbody))]
public class PlayerDoubleJump : MonoBehaviour
{
public float jumpForce = 5;
public float groundDistance = 0.5f;
Rigidbody rigidBody;
bool canDoubleJump;
void Awake()
{
rigidBody = GetComponent<Rigidbody>();
}
/*bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down,
groundDistance);
}*/
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (Physics.Raycast(transform.position, Vector3.down,
groundDistance))
{
rigidBody.velocity = Vector3.up * jumpForce;
canDoubleJump = true;
}
else if (canDoubleJump)
{
rigidBody.velocity = Vector3.up * jumpForce;
canDoubleJump = false;
}
}
}
}