Coder Social home page Coder Social logo

Dynamically Changing a Collider about nez HOT 12 CLOSED

prime31 avatar prime31 commented on May 28, 2024
Dynamically Changing a Collider

from nez.

Comments (12)

prime31 avatar prime31 commented on May 28, 2024

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.

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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.

from nez.

prime31 avatar prime31 commented on May 28, 2024

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.

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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.

from nez.

prime31 avatar prime31 commented on May 28, 2024

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

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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.

from nez.

prime31 avatar prime31 commented on May 28, 2024

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.

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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

from nez.

prime31 avatar prime31 commented on May 28, 2024

Found it!

colliders

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

from nez.

bojjenclon avatar bojjenclon commented on May 28, 2024

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.

from nez.

prime31 avatar prime31 commented on May 28, 2024

Glad to hear it did the trick!

from nez.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.