简体   繁体   中英

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. I have attached rigidbody to my player, have instantiated it. 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. The interesting part is that Debug.Log("jump") shows on the console, but the player doesn't want to jump. Method void CollisonOther() is never called. I have also tried to change velocity and use transform.translate , no result

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. 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. As written, onCollisionEnter will never be entered.

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.

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.

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. If your 'OnGround' Layer check is perfect it should work.

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).

For some reason, it interfered with any key inputs I checked for in the code (in your case GetKey(KeyCode.Space)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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