Hi guys,
New here, looking for some advice over an issue i’ve been pulling my hair out for hours over.
I’ve applied velocity to my 2D sprite character to move it.
With interpolation applied to the rigidbody2D, the movement is smooth as silk. This is great. Without interpolation, the movement is jittery.
One of its abilities requires it to rotate. I rotate it using:
this.transform.Rotate(0, 0, -180);
Unfortunately, the character changes position slightly every time i rotate (rotation amount does not matter. even 1 degree causes it to move.)
Turning off interpolation fixes this, but causes the unwanted jittery effect.
Turning off velocity fixes this, but of course now my character cannot move.
Does anybody know how I may solve this?
Any help is appreciated!
Try adding torque instead of rotating it: Unity - Scripting API: Rigidbody2D.AddTorque
Or rotate the rigidbody and not the transform: Unity - Scripting API: Rigidbody2D.rotation
1 Like
You are brilliant. I didn’t even know you could rotate the rigidbody. Works like a charm.
I seem to be getting the same issue with changing the scale. im doing
this.transform.localScale = new Vector3(0.8f, 0.8f, 1);
Can’t find any other way to change the scale in the scripting API, unfortunately. Anybody have an idea?
Scaling colliders in real-time has always been problematic. It’s just how physics engines work. I believe it has to recalculate the object for every change in scale. When you change scale, you’re essentially creating a new collider at a new size at every step. Then physics has to recalculate the rigidbody information as well. You probably would get some odd results if you tried colliding objects against that scaling collider too, since it’s essentially popping into a new shape every frame, there’s no velocity or force associated with that. In most cases, it would probably just force a solid object out of the way with unrealistic force.
That is an issue indeed. Thanks for your support, Guavaman. The solution I ended up going with, was creating the sprite as a child of a game object. The game object had the scripts for velocity etc applied to it, and the sprite sat within that. Seems to have fixed my issue. Hope this helps anybody with a similar problem!