由于某些原因,对撞机会碰撞,我会飞翔

问题描述 投票:1回答:1

我正在制作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;

这是我的一些代码。我试图这样做,以便如果我放开空气然后再次按下它,将不会发生任何事情。它可以工作,但是有一个小问题...

通过将一个对撞机(头部)撞向另一个对撞机(天花板),头部进入身体对撞机。这使对撞机检测认为总是有一个对撞机在碰,而我却坚持。这意味着我可以跳大约预期高度的5倍。另一个副作用是,有时似乎有一个力像风一样作用在物体的一侧。这并非总是会发生。

如何彻底删除该错误?我唯一的选择是使角色具有1个对撞机?

c# unity3d 2d collision collider
1个回答
0
投票

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

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

此外,我的AirDetection有缺陷,但我已修复该问题,因此一切顺利!但是,现在,如果我握住它并跳跃(或跳,放开,然后再次击中,然后再撞上地面),我会再跳1.8 / 1.9单位。没关系。

© www.soinside.com 2019 - 2024. All rights reserved.