【问题标题】:Player won't jump in unity2d玩家不会在 unity2d 中跳跃
【发布时间】:2021-04-13 15:40:43
【问题描述】:

这是我在 void Update 中更新的跳转代码

 void Jump()
{
 if(isgrounded == true)
 {
 amountofjumps = jumps;    
 }
 if(Input.GetKeyDown(KeyCode.UpArrow) && amountofjumps > 0)
 {
 rb2d.velocity = Vector2.up * jump * Time.deltaTime;
 amountofjumps--;
 }
 else if(Input.GetKeyDown(KeyCode.UpArrow) && amountofjumps == 0 && isgrounded == true)
 {
 rb2d.velocity = Vector2.up * jump * Time.deltaTime;    
 }
 
}

这是我用于跳转代码的变量

bool isgrounded;
public float groundcheckradius;
public LayerMask whatisground;
public float jump;
private int amountofjumps;
public int jumps;

这是我检测地面的方法

 void checkforground()
{ 
isgrounded = Physics2D.Raycast(transform.position,Vector2.down, groundcheckradius,whatisground);
   
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, transform.position + Vector3.down * groundcheckradius);  
}

提前致谢

【问题讨论】:

  • 你的变量的初始值是多少?
  • 初始值是什么意思?
  • 变量将被设置为的第一个值
  • bool isgrounded = false groundcheckradius = 0.6 float jump = 67 int jumps = 1

标签: unity3d


【解决方案1】:

Velocity 常用于移动物体,试试rigidbody2d.AddForce()

void Jump()
{
    if (isgrounded == true)
    {
        amountofjumps = jumps;
    }
    if (Input.GetKeyDown(KeyCode.UpArrow) && amountofjumps > 0 && isgrounded == true)
    {
        rb2d.AddForce(transform.up * jump, ForceMode2D.Impulse);
        amountofjumps--;
    }
}

【讨论】:

  • 所以跳跃不动? ^^
【解决方案2】:
  • 首先Time.deltaTime 在设置velocity 时毫无意义。 velocity 已经与帧速率无关,因此当您将速度乘以 Time.deltaTime(对于 60 fps 约为 0.017)时,它会变得非常小/慢。

  • 其次,当前您会覆盖整个速度,因此如果您的玩家向前移动,它将完全停止 X 轴上的任何移动。

  • 最后,当你被禁足时,你希望总是能够跳跃......不仅是amountofjumps == 0,因为在你之前永远不会如此将其设置为amountofjumps = jumps;!从地面跳下时你根本不想检查amountofjumps

你可能宁愿使用例如

// get the current velocoty of the rigidbody
var velocity = rb2d.velocity;
// Only overwrite the Y velocity with the jump
velocity.y =  jump; 
// re-assign the changed vector to the rgidbody
rb2d.velocity = velocity;

然后我会将逻辑更改为例如

private void Jump()
{
    if(isgrounded)
    {
        amountofjumps = jumps; 
    
        // when you are grounded you can always jump!
        // Not only when the amountofjumps == 0
        // actually when you are grounded the amountofjumps shouldn't matter at all
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            DoJump();
        }  
    }
    // As before while not grounded the amountofjumps is taken into account
    else if(amountofjumps > 0)
    {
        if(Input.GetKeyDown(KeyCode.UpArrow))
        {
            DoJump();
        }
    }
}

private void DoJump()
{
    // get the current velocoty of the rigidbody
    var velocity = rb2d.velocity;
    // Only overwrite the Y velocity with the jump
    velocity.y =  jump; 
    // re-assign the changed vector to the rgidbody
    rb2d.velocity = velocity;

    // Always reduce the amount of jumps also when jumping from the ground
    amountofjumps--;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多