I have a objcet with a normal RigidBody2D and a capsulecollider2d
When I use RigidBody2D to change the rotation of an object, the collider attached to it doesn’t rotate accordingly. However, when I manually change the rotation in the inspector, the collider resets to the correct rotation. Additionally, I have not enabled the Rotation Freeze option.
Are there any better methods besides changing the Transfroms?
I used a RigidBody2D and a (polygon) collider 2d. I just use the transform of the RectTransform to modify the rotation and position. From my experience, the collider 2d just follows the position & rotation of the RectTransform…
I probably need code or more details to help you out here…
Colliders don’t move on their own, their position/rotation is entirely defined by the body (Rigidbody2D). They cannot “move” independently. Maybe the capsule is on a child GameObject and you’re stomping over its rotation in a script/animation; something you should not be doing.
You don’t need “better methods”, something else in your set-up is going wrong. Might be better to show the component hierarchy/set-up and any code that is modifying it.
Start a new scene, add a Rigidbody2D and a CapsuleCollider2D and set the Rigidbody2D rotating, you’ll see the collider rotate. If it’s not doing it in your other scene then start disabling stuff.
Thank you for your reply. I’ve found out the reason for it. When my object starts rotating around the Y-axis with an angle of 180 degrees, the rotation of the object itself becomes very fast, but the collider remains motionless. However, when the rotation angle around the Y-axis is set to 0 degree, the collider can spin successfully.(I just use the default CapsuleCollider2d and Rb , RB.MoveRotation(Time.time*5)
It’s 2D physics, you shouldn’t be rotating in any axis other than the Z axis and the Rigidbody2D does that.
Note that all you’re doing here is causing the collider to be recreated from scratch each time, 2D colliders cannot actually be rotated into 3D space so they end up skewed/scaled in the 2D XY plane. Changing this should only ever be done as an authoring/edit thing, doing it at runtime isn’t a good idea.
Oh, I change the Y-axis just to change the orientation of the object. This isn’t something that runs all the time. There shouldn’t be any problem with it like this, right?
Are you rotating around the Y axis to flip it horizontally? If so, you might want to consider using the X local-scale to do this i.e. make it -1 or 1. Also, only do this to the visuals (put them on a different child GameObjects); there’s no reason to flip a CapsuleCollider2D and it’ll only cause it to be recreated. Also to note, there’s a dedicated FlipX/Y on the SpriteRenderer2D you can use if it’s all you need.
If it’s an infrequent event then you should be okay though. I only mention it because I see devs constantly changing the Transform like this and it causes issues because the colliders are being recreated each time.