Hello, i want to change the collider of my player object that has a rigidbody at runtime. But if i just disable the old and enable the new collider there are some weird physics as if the disabled collider is still effecting the physics. Fo i have to clear some states or what is the proper way to handle this?
You might want to use a Compound Collider on a single parent rigidbody and enable/disable child colliders. How to create. (Unity - Manual: Compound colliders)
Compound colliders are collections of multiple colliders on a single Rigidbody GameObject . Each collider is attached to a child GameObject, with the Rigidbody GameObject as its parent. This allows you to group and arrange colliders into complex shapes, and have the physics system treat them as a single rigid body.
Thanks for the answer but i only have one collider active at a time. So it kinda work but somehow if i switch from cube to sphere for examples it does not roll perfect as it did before i switched between colliders at runtime. I guess that maybe the calculated weight is reused or something else gets not cleaned
There’s nothing that needs clearing, Components lose their state when removed.
Though the rigidbody itself may still have velocities, ie a sphere will be rolling and thus accelerate angular velocity. If you switch to a box collider, that angular velocity will affect the collider differently because for a box, there is no such thing as “rolling”.
Generally, replacing one collider with another at runtime is sort of an unsupported operation because it can easily lead to situations that wouldn’t normally occur. Say the sphere is rolling through a tube that just fits the sphere. Now you switch to box collider, and the edges of the collider are suddenly stuck in the tube’s colliders. This can jiggle the box around, even to the point where it leaves the tube altogether.
So it’s kind of expected that a change in colliders will cause the current motion to … be erratic. With the sole exception of changing colliders while the object is entirely in mid-air.
Thanks, i extpressed myself unclearly so i try again. Obviosly a Cube behaves different then a sphere thats what i want. The Problem is that if i have multiple Colliders for example a sphere and a box collider on the same object and i enable the sphere Collider and disable the box collider. It not behaves like a sphere. So it seems like somehow the disabled box collider is still used in calculation or there is something saved from when the box collider was enabled and the sphere collider was disabled
Ok it’s weirder than i thought. The Probelm is i’m adding 2 MeshCollider Components in Start when the sphere gets spawned and i disable than directly. Even though they are disabled in the Inspector they behave like there are still active. After i reanable and disable them from the inspector it works. So do i have to wait before disabeling added Colliders or is there something else i am mising?
You might need to add soft bodies—if they fit your game concept. For the collider itself, you can assign meshes in the script and update the mesh collider’s mesh through code:
MeshCollider.sharedMesh = mesh;
Additionally, removing the velocity while morphing might be a good idea to prevent any unexpected behavior.
Try toggling isTrigger on/off in the collider.