由于某种原因,碰撞器碰撞,我可以飞 [英] For some reason, colliders collide, and I can fly

查看:47
本文介绍了由于某种原因,碰撞器碰撞,我可以飞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

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

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.

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

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

推荐答案

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

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;

此外,我的 AirDetection 存在缺陷,但我修复了它,所以一切顺利!但是,现在,如果我握住它并跳跃(或跳跃,放手,然后在击中地面之前再次击中),我会额外跳高 1.8/1.9 个单位.不过没关系.

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.

这篇关于由于某种原因,碰撞器碰撞,我可以飞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆