#Switching between CharacterBody2D and RigidBody2D

1 messages · Page 1 of 1 (latest)

abstract sequoia
#

hi, im working on a game in C# where the central mechanic involves switching between regular input controls for character movement, and rolling around as a ball while following regular physics-based movement. i have a really messy solution that kind of works, but i feel like i could be doing something much cleaner. i was thinking something like using a state machine, but im not quite sure what the best way to handle the implementation would be. any ideas or advice would be appreciated!!

crimson shuttle
#

what does your current solution look like?

abstract sequoia
#

at a high level:

currently i have two separate node instances: a characterbody2d and a rigidbody2d

in the main scene script (though i realize i should probably just use a separate scene to do this other than the main one): when space bar is pressed i call a function on the characterbody2d to play an animation, hide it and disable its hitbox. then the main scene calls a function on the hidden and disabled rigidbody2d to reposition it to the same position (using the integrateforces function) as the characterbody2d, play an animation, show it, and then re-enable its processing and its hitbox.

when i let go of space bar, it kind of does the same thing but in reverse: plays an animation from the rigidbody2d, disables its hitbox and processing, hides it, then repositions the characterbody2d to the same position that the rigidbody left off, plays an animation, and then shows and re-enables its hitbox

#

it works, but i feel like theres probably a better way to handle it that im not seeing

crimson shuttle
#

well yeah that seems like a good solution. not sure what you need help on exactly, then, other than the specific implementation. for that, you could define a PhysicsType enum that has two possible values, one for the CharacterBody2D mode and the other for the RigidBody2D mode. then, a private variable to keep track of the current mode, and a PhysicsType setter/getter field in the player script that, when set, sets said private variable to the value and does the disabling/enabling and repositioning and such (the logic of which is probably best defined within a separate function)

#

unless you want to do the same thing without having to switch physics body types, in which case you'd have to implement physics for the characterbody, or have to calculate the forces required to keep a rigidbody2d upright and moving the direction you want it to. i don't think the effort required to do this would be worth it

abstract sequoia
abstract sequoia
crimson shuttle
#

you're welcome! i will say that i did have some interest in this problem mainly because i've programmed rigidbodies that are controllable like character bodies. i liked how it was a bit "wobbly", but it definitely wasn't easy.

abstract sequoia
#

yeah i really like the idea of things being controlled by actual physics, like a character or enemy becoming a ragdoll when they get hit by something