For some reason, colliders collide, and I can fly

余生颓废 提交于 2020-01-05 04:11:14

问题


I am making a small prototype of a Unity 2D game. I have 2 colliders for my character. This helps me because I want my character to be able to wall jump. This means that when I collide with the wall, my collider detection script assumes that I am hitting 2 colliders, and helps me customize animations.

     //on the ground, change y position as necessary, and if you are pressing up, held = true
     if (InAirDetection.AirDetect == 1)
     {

         position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
         if (Input.GetButton("Vertical") == true)
         {
             held = true;
         }
         position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
     }

     //on the wall, change y position as neccessary, and if you are pressing up, held = true
     if (InAirDetection.AirDetect == 2)
     {

         position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
         if (Input.GetButton("Vertical") == true)
         {
             held = true;
         }
     }


     //in the air, if held is true, change y. If you aren't pressing up, held is false.
     if (InAirDetection.AirDetect == 0)
     {
         if (held == true)
         {
             position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;
         }
         if (Input.GetButton("Vertical") == false)
         {
             held = false;
         }
     }

 }
 // apply the transformations
 transform.position = position;

This is some of my code. I tried to make it so that if I let go of up in the air and pressed it again and nothing would happen. It works but there is a slight problem...

By slamming one collider(head) against the different collider(ceiling), the head goes into the body collider. This makes the collider detection thinks there is always one collider touching and that I am holding up. That means that I can jump about 5 times the intended height. Another side effect is that sometimes there seems to be a force acting on one the side of the object, like wind. This doesn't always happen.

How can I remove the bug completely? Is my only option to make the character have 1 collider?


回答1:


I had an extra line of code. Also, I changed the collider's position a tad bit. This was the extra line:

position.y += MoveUnitsPerSecond * verticalInput * Time.deltaTime;

Also, my AirDetection was flawed, but I fixed that, so everything's good to go! Now, however, if I hold it and jump(or jump, let go, and hit up again before hitting the ground), I jump an extra 1.8/1.9 units higher. That's ok though.



来源:https://stackoverflow.com/questions/59440776/for-some-reason-colliders-collide-and-i-can-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!