出于某种原因,对撞机发生碰撞,我可以飞了

For some reason, colliders collide, and I can fly

我正在制作 Unity 2D 游戏的小型原型。我的角色有 2 个碰撞器。这对我有帮助,因为我希望我的角色能够跳墙。这意味着当我与墙壁发生碰撞时,我的碰撞器检测脚本假定我正在撞击 2 个碰撞器,并帮助我自定义动画。

     //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;

这是我的一些代码。我试图做到这一点,如果我在空中松开并再次按下它,就不会发生任何事情。它有效,但有一个小问题...

通过将一个对撞机(头部)撞击另一个对撞机(天花板),头部进入 body 对撞机。这使得对撞机检测认为总是有一个对撞机在接触并且我正在阻止。这意味着我可以跳到预期高度的 5 倍左右。另一个副作用是有时似乎有一种力作用在 object 的一侧,例如风。这并不总是发生。

我怎样才能彻底消除错误?让角色拥有 1 个对撞机是我唯一的选择吗?

我多了一行代码。另外,我稍微改变了对撞机的位置。 这是额外的行:

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

此外,我的 AirDetection 存在缺陷,但我修复了它,一切顺利! 但是,现在,如果我握住它并跳跃(或跳跃、松开并在触地前再次向上击打),我会额外跳高 1.8/1.9 个单位。不过没关系。