weixin_39843986
2020-12-30 10:21 阅读 1

Dynamically Changing a Collider

I have certain animations that require the collider on an entity to change with each frame. There doesn't seem to be any obvious way to modify an existing collider, as all of my experiments have yielded... odd results. This mostly seems to be due to the way colliders are queued up to be added, rather than being registered immediately. Is there an easy way to modify a collider that I'm simply overlooking, or is that behavior simply not intended in the current state of the framework?

该提问来源于开源项目:prime31/Nez

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

12条回答 默认 最新

  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Indeed Collider adds are queued up to happen in the next frame much like Entity and Component adds. This allows multiple Colliders/Entity/Component additions to happen in one frame keeping the state consistent so that the newly added objects work properly.

    One possibility is to add all your colliders at creation time then change their physicsLayer to an unused layer. When you want to activate the collider change it's physicsLayer to the appropriate layer toggling the current Collider to the unused layer. You can then essentially toggle the colliders on/off at will.

    I am personally not a big fan of doing things this way but it may work fine for your specific situation. Issues can arise when the Collider is "turned on" if it happens to be overlapping other Colliders. It requires a bit of defensive coding to keep things stable.

    An alternative that may work for your situation (I use this a lot for stuff like punches, kicks, sword swings, fast actions that only last a few frames) is to not use a Collider directly at all and instead when the animation frame happens query the Physics class directly.

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    For actual attack collisions I already use the Physics class (which is awesome and works great btw), but the colliders I'm talking about here are for the player itself. Particularly for colliding with terrain and enemies. Certain animations, such as evading and jump kicking, need to alter the player's hit box. I want it to be a mechanic that these animations can potentially reach areas that a normal move or jump wouldn't be able to reach. Ideally I was going to have the main collider change for every frame of the animation, but if that's not possible I suppose I can just create a collider for each animation and switch them when the animation is played.

    点赞 评论 复制链接分享
  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Do you need to change the colliders Shape or just size? If just the size needs to be changed you should be able to do that. If the Shape needs to change that is where multiple Colliders would be required.

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    Just the size. There are a lot of variables packed in a collider and I honestly wasn't sure which to change to modify the size and position without messing up its interaction with the physics system. I don't remember which ones I tried modifying before, but I remember seeing that width/height would be modified in some areas of the collider and not others, making things inconsistent. The collider will always be a BoxCollider, I just need to adjust it to the size/origin of the current animation.

    点赞 评论 复制链接分享
  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Let me give it a look. You should be able to change width/height/offset and it will auto update itself in the SpatialHash.

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    Thanks for the help, sorry for all the questions.

    I'm currently experimenting with changing the width, height, and origin of the collider as well as making a new Box shape and changing its position, which seems to adjust the actual size and position properly (if the debug shape outline is any indication) but afterward my player jumps around until it flies off the screen when I move it (using the Mover utility).

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    I should note my animations might be set up a bit different from what you're used to, as each frame of the animation has an adjusted origin upon creation so that it all lines up properly when played. I don't know if that would affect the way calculations are done at all or not.

    点赞 评论 复制链接分享
  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Sprite origin is a separate thing so it shouldn't have any effect on the collider. It definitely sounds like something odd is going on. I'll see what I can find with some testing.

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    Ah, okay. Kinda figured as much but I wasn't sure.

    Alright, hopefully it's something minor (like me just doing something wrong). Thanks again for the help, I'll leave you to it until you have some suggestions (or potentially a bug fix if there is one).

    点赞 评论 复制链接分享
  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Found it!

    colliders

    The stupid Box wasnt updating its actual verts when width/height changed.

    点赞 评论 复制链接分享
  • weixin_39843986 weixin_39843986 2020-12-30 10:21

    You rock, that works perfectly. Such a simple fix, but it's always the simple ones that slip past us, aye? Thank you so much for the help! :D

    You'll hear from me again if something goes awry in the future. :P Great framework, thanks for making it available to everyone like this.

    点赞 评论 复制链接分享
  • weixin_39625337 weixin_39625337 2020-12-30 10:21

    Glad to hear it did the trick!

    点赞 评论 复制链接分享

相关推荐