- Home /
How can I animate 2d sprite rotation without interpolation?
I would like to animate a weapon being swung in 2 frames - all I'm doing is changing the position and rotation of the weapon sprite. However, I don't want unity to interpolate the rotation (generate sub-frame rotations that smooth out the animation).
How can I do this? Using flat curves does not work, and my options seem to be Euler interpolation and Quaternion interpolation (there is no none).
I would like to not have to make spritesheets for every frame of every weapon.
Answer by JimMakesGames · Sep 05, 2014 at 09:55 AM
I know it's a bit late but I came across this thread because I was having the same problem, and I've come up with a solution that's a bit weird but seems to work and doesn't require manually inputting the angles for each frame:
public class WeaponRotationFix : MonoBehaviour {
float correctRotation;
bool stopRotation;
// occurs after animation
void LateUpdate()
{
// set the correct rotation when the stopRotation has been set by animation event
if (stopRotation)
{
correctRotation = transform.eulerAngles.z;
stopRotation = false; // unset so this only calls at the animation event
}
// set rotation each frame to the correct rotation
transform.localRotation = Quaternion.Euler(0, 0, correctRotation);
}
// called by animation event
public void StopRotation()
{
stopRotation = true;
}
}
As long as you add an animation event for each frame calling StopRotation() then it works and you get nice discrete frames rather than interpolated sprites. Hope that's useful to someone.
Answer by Loius · Jul 06, 2014 at 06:39 PM
Right-click key, Both Tangents -> Constant
Constant means that the key's value will be constant from this key to the next. You can also keep a key constant on just one tangent (one side) so it will lerp TO or FROM the key, but remain constant elsewhere.
That doesn't work. From the manual:
When Euler Angles interpolation is used, Unity internally bakes the curves into the Quaternion representation used internally. This is similar to what happens when importing animation into Unity from external programs. Note that this curve baking may add extra keys in the process and that tangents with the Constant tangent type may not be completely precise at a sub-frame level.
With rotation (not position), there is always sub-frame tweening going on, which I do not want.
Well that's incredibly crappy, isn't it? It's good enough for my purposes but there's no way that's going to be okay for what you're looking to use it for.
How about animation events? Create a function to get called by the animation:
public void SetZRotation(float value) {
transform.rotation = Quaternion.Euler(Vector3.forward * value);
}
Then you can call that through an animation event at the right time and force the object to the correct rotation instantly.
I generally defend Unity's decisions like a madman but it seems pretty silly that the 'constant' option isn't actually constant.
This should work, although the object being animated has no script attached and I wanted to avoid having to attach one.
I came up with an alternative solution here, but I'll suck it up and use this one since it seems far more elegant.
Posted my solution below. Just animate the custom property that will be set to rotation.z angle each frame.
Answer by soulburner · May 30, 2015 at 12:10 PM
My solution is:
make a public float property named "z_rotation"
do in each frame:
transform.localRotation = Quaternion.Euler(0, 0, z_rotation);
animate my "z_rotation"
Answer by Deepscorn · Sep 16, 2015 at 03:35 PM
I'll make an example of one dirty hack, which is available because of animation events. Suppose, we need to flip sprite horizontal in our animation. Write method:
void FlipHorizontal()
{
animator.transform.Rotate(0, 180, 0);
}
And add events to animation which will call that method. That's all. No interpolation occurs
Your answer
Follow this Question
Related Questions
Scissor tool with 2d sprite 0 Answers
Reverse animation loop stops at frame zero 0 Answers
Play Two Animations Simultaneously using Animator 2 Answers
Animation is not playing again in Unity 4.3 1 Answer
Unity 2d animation acting strange 0 Answers