简体   繁体   中英

Property 'BoxCollider' does not move when force is added to character's rigidbody (rigidbody.AddForce)

I was trying to add a jump function into the game by having the character have a jumping animation and use the 'rigidbody.AddForce' function to actually make it jump.For the character to return back to its idle position, it would need to collide with anything around to turn the 'isJumping' bool false, making the character idle again. However, I noticed that there would be this glitch where the character's walking animations would loop when landed. I later saw that the BoxCollider did not move upwards with the character, therefore making every collision based event impossible to trigger.

I initially tried to tie the BoxCollider with.AddForce (Player.BoxCollider.AddForce()), but to no avail. I also tried to turn off the BoxCollider and turn it back on again, but that method also didn't work.

Here's the code for the Jumping command:

    if (Input.GetKeyDown(KeyCode.Space) && isJumping == false)
    {
        isJumping = true;
        thePlayer.GetComponent<Animator>().Play("Jump");
        rb.AddForce(new Vector3(0, 4, 0), ForceMode.Impulse);
        rb.AddForce(new Vector3(0, -4, 0), ForceMode.Impulse);
    }

And this is the code for any kind of collisions:

   void OnCollisionEnter(Collision col)
{
    isJumping = false;
}

I expect the isJumping trigger to be considered false.

Your player will eventually come down because it has rigidbody component attached to it, you don't need to addforce to comes it down

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