- Home /
Non-rotating collider fixed to a rolling ball
So, in my game the player is basically on top of a ball that gets rotational acceleration applied to it and rolls. The ball has a rigidbody and a sphere collider, and I like the movement of the ball so far.
I would like the player to have a collider so the ball can't roll under a gap or something and have the camera pass through walls. I'm having trouble with the collider representing the player (the capsule on top of the ball).
Because the ball rotates, I can't simply make the collider a child of the ball, as the collider would rotate with it. I've tried using scripts to fix its rotation and position above the ball every Update and FixedUpdate, but it either creates horrible jitter or just passes through walls as if the collider isn't there.
The closest I've come is by using joints. I've managed to fix the position and rotation above the ball, but the problem with this is a rigidbody has to be attached to the capsule, and so it has to have mass. With a mass comparable in size to the ball's, the ball has to pull along the capsule and starts and stops sluggishly. If the mass of the capsule is very low, at high speed collisions it just gets pushed around and doesn't stay where I'd like it to be:
I've messed around with articulation bodies, but they don't seem to have options that allow the collider to stay where it is relative to the parent and not rotate.
So, does Unity have some feature that would allow my capsule collider to stay with the rotating object in this way? If not, I will get to scripting and try to work something out with Physics.OverlapCapsule or something. Just want to check if anyone can help me with a solution I haven't considered.
Thanks
Answer by AlgoUnity · Sep 24, 2021 at 07:03 AM
Thats an interesting problem I feel like there must be a good solution, at first I was thinking to have a parent object that both the ball and capsule are attached to, but you want to use the angular velocity of the ball as the means of movement which makes things complicated.
I do have a crazy idea though, what if you attach 4 children to the capsule and put box colliders on them, these boxes would basically surround the ball such that they are barely outside of its reach and aren't colliding with the ball. Then you make these box colliders not be able to collide with anything other than the ball using layers, so they can go through walls and everything else but the ball cannot go through them. That way when the capsule hits something, the ball gets stopped by the 4 box colliders under the capsule and the ball will never be able to move away from under it.
Seems a bit hacky but that's all I can think of.
Huh, that is a wacky but clever solution, lol. I've made considerable progress on this through scripting (basically, checking if the capsule is overlapping with anything, manually setting the corrected position, linear/angular velocity of the ball, and calling Physics.SyncTransforms()). There's a little bit of jitter, but with some tweaking it could work.
As for a solution using colliders, yeah, I feel like there should be some simple way of doing it, but I'm just not seeing it. I will definitely give your box method a go and report back. Thanks!
Unfortunately, no matter how I meddle with the settings of the box colliders, the ball just passes right through them if they're children of the capsule.
Despite the fact that if I separate the boxes from the capsule, the ball collides with them just fine.
Good idea, though.
I think you might be able to have the boxes use a separate RigidBody from the capsule and have them follow the capsule with a script on FixedUpdate, instead of being children of the capsule. That just makes the solution even more hacky though haha
Your answer
Follow this Question
Related Questions
NavMeshAgent with Non-Kinematic Rigidbody possible? 0 Answers
Get angle from impact 2 Answers
Odd collider rotation 1 Answer
How to roll a ball along a curved tube. 2 Answers
Animal Rigidbody and Collider problem 0 Answers