简体   繁体   中英

Unity character goes flying

So I am working on the jumping of my character at the moment, and I have made the jumping work as I want it to, except that it does not check for ground. I could easily make a bool that goes true and false on collision with an object tagged ground, but I would like to do it with the "Character Controller" component, since I would like to use it for other features.

But when I add the Character Controller, the second I move my player, he goes flying with about 100000 km/h...

How do I avoid this? Could someone fix my script, and explain the thought process of why it happens?

Jumping part of my script:

void update () {
    if (rb.velocity.y < 0) {
        rb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    } else if (rb.velocity.y > 0 && !Input.GetButton ("Jump")) {
        rb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
    }

    //Hoppe funktionen
    if (Input.GetButtonDown("Jump")) {
        GetComponent<Rigidbody> ().velocity = Vector3.up * jumpForce;
        //Debug til hop
        //Debug.Log("Surface is jumpable");
    }
}

Also I would really like if my character would keep the momentum in the air, so I am not able to turn as easily in while airborn, but instead keeps moving the direction I jumped.

Based on what you said I'm assuming the code you're showing is the working one and the addition of the Character Controller script is what bugs it?

The 'flying' bug is pretty common and can be a multitude of things. Check for the following:

  1. Multiple Physics Colliders that are not triggers
  2. Multiple rigidbodies (in your code you seem to be referencing two different ones possibly -variable rb and GetComponent)
  3. Updating the force every frame - sometimes it's a sneaky IF statement that bugs it out due to it receiving the incorrect data

I could easily make a bool that goes true and false on collision with an object tagged ground, but I would like to do it with the "Character Controller" component, since I would like to use it for other features.

Why not just do this in the Character Controller itself?

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