简体   繁体   English

Unity 3d 播放器跳转不起作用,rb.addforce 不起作用

[英]Unity 3d player jump does't work, rb.addforce doesn't work

I have a very basic infinite runner and I want to make my player jump.我有一个非常基本的infinite runner ,我想让我的球员跳跃。 I have attached rigidbody to my player, have instantiated it.我已将rigidbody附加到我的播放器上,并对其进行了实例化。 here is some part of my code这是我代码的一部分

 void FixedUpdate()
{
    if (OnGround)
    {
        if (Input.GetKeyDown(KeyCode.Space))
        { 
            rBody.AddForce(new Vector3(0, 10, 0), ForceMode.Impulse);
            Debug.Log("jump");
            OnGround = false;
        }
    } 
}     

void onCollisionEnter(Collision other)
{
    Debug.Log("collision");
    if (other.gameObject.CompareTag("ground"))
    {
        OnGround = true;
    }
}

I have tried to put this part in Update() , no result.我试图把这部分放在Update() ,没有结果。 The interesting part is that Debug.Log("jump") shows on the console, but the player doesn't want to jump.有趣的部分是Debug.Log("jump")显示在控制台上,但玩家不想跳转。 Method void CollisonOther() is never called.方法void CollisonOther()永远不会被调用。 I have also tried to change velocity and use transform.translate , no result我也尝试过改变velocity并使用transform.translate ,没有结果

jump.跳。 How can I make it jump?我怎样才能让它跳起来?

There are a couple of things that could be the issue.有几件事可能是问题所在。 Make sure the isKinematic box is unchecked.确保未选中 isKinematic 框。 It's also possible that the character is jumping but you cannot see it if your sprites were imported too big (ie: it's jumping a very very small amount relative to the size).也有可能角色正在跳跃,但如果您的精灵导入太大,您将看不到它(即:相对于大小,它的跳跃量非常小)。 You could try printing some more debug statements of the character's position to see if it is actually changing or not.您可以尝试打印更多角色位置的调试语句,以查看它是否确实在更改。

OnCollisionEnter should have a capital O instead of lower case. OnCollisionEnter 应该有一个大写的 O 而不是小写。 As written, onCollisionEnter will never be entered.正如所写,永远不会输入 onCollisionEnter。

That's the only code problem that could be wrong, assuming you actually assigned the other variables corrrectly.这是唯一可能出错的代码问题,假设您实际上正确地分配了其他变量。 However, there could be a variety of game object related behavior.但是,可能存在各种与游戏对象相关的行为。

Is the ground tag applied to the game obejct that you think your runner is colliding with?地面标签是否应用于您认为跑步者正在碰撞的游戏对象? If not, it won't jump.如果没有,它不会跳。

Is your mass for the jumper's rigidbody very high?跳跃者刚体的质量是否非常高? With the default of 1, it jumps pretty high, but could not get the right amount of force if it was a larger mass.默认值为 1,它跳得相当高,但如果质量较大,则无法获得适当的力。

it seems to be correct in your code but don't know why not jumping.它在您的代码中似乎是正确的,但不知道为什么不跳。 BTW, you can try with this code just without AddForce and changing the checking you are using.顺便说一句,您可以在没有 AddForce 的情况下尝试使用此代码并更改您正在使用的检查。

if (Input.GetButtonDown("Jump") && OnGround)
{
    rBody.velocity = new Vector2(rBody.velocity.x, jumpSpeed);
}

Make sure it is under your Update method coz Input calls in Update method.确保它在您的 Update 方法下,因为 Input 在 Update 方法中调用。 If your 'OnGround' Layer check is perfect it should work.如果您的“OnGround”层检查是完美的,它应该可以工作。

I ran into the same issue and, after hours of investigating, in my case it had nothing to do with the code, but with the below Unity bug (still present in 2020.1.6f1).我遇到了同样的问题,经过数小时的调查,在我的情况下,它与代码无关,而是与下面的 Unity 错误(仍然存在于 2020.1.6f1 中)有关。

For some reason, it interfered with any key inputs I checked for in the code (in your case GetKey(KeyCode.Space)出于某种原因,它干扰了我在代码中检查的任何键输入(在您的情况下为 GetKey(KeyCode.Space)

https://issuetracker.unity3d.com/issues/urp-errors-are-constantly-thrown-when-active-input-handling-is-set-to-input-system-package https://issuetracker.unity3d.com/issues/urp-errors-are-constantly-thrown-when-active-input-handling-is-set-to-input-system-package

The fix was going to Edit - Project Settings - Active Input Handling - select Both.修复程序是编辑 - 项目设置 - 活动输入处理 - 选择两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM