我目前正在开发一款 2d 游戏,为了跳跃,我正在使用
Rigidbody.Addforce()
。但是,这会导致我的播放器缓慢着陆。我希望它尽快降落。
我尝试操纵 Gravity scale 和 mass 的值,但这会对玩家的移动产生影响,而且跳跃效果也不好。
这是代码:-
private void Update()
{
if (IsGrounded == true)
{
extraJumps = 1;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && PlayerCanMove == true) //Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended
{
if (CheckGrounded() == true && OnSlope() == false)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else if(CheckGrounded() == true && OnSlope() == true)
{
Catanim.SetBool("isJumping", true);
IsJumping = true;
RBody.AddForce(Vector2.up * JumpForce * 1000f);
extraJumps = extraJumps - 1;
}
else
{
IsJumping = false;
Catanim.SetBool("isJumping", false);
}
}
}
}
如果您正在尝试制作 2D 游戏,我建议您使用角色控制器并设置重力。但是,如果你想坚持刚体并想调整你的重力,你可以这样做:
public float gMoon=1.6f; //gravity on the moon.
void Start()
{
rb=GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rb.AddForce(new Vector3(0, -1.0f, 0)*rb.mass*gMoon);
}
您可以使用刚体的速度场,如统一文档中所示https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
设置为正值跳跃,一旦在空中,您可以将其设置为负值以向下移动角色。但这只能让你垂直跳跃,否则你必须做一些简单的数学运算才能做出弯曲的轨迹。