Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kyuugatsu · Jul 06, 2014 at 06:23 PM · animation2d

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5

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.

alt text


image1.png (35.3 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kyuugatsu · Jul 06, 2014 at 07:04 PM 2
Share

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.

avatar image Loius · Jul 06, 2014 at 10:29 PM 0
Share

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.

image1.png (23.2 kB)
avatar image Kyuugatsu · Jul 06, 2014 at 11:51 PM 0
Share

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.

avatar image soulburner · May 30, 2015 at 12:11 PM 0
Share

Posted my solution below. Just animate the custom property that will be set to rotation.z angle each frame.

avatar image
0

Answer by soulburner · May 30, 2015 at 12:10 PM

My solution is:

  1. make a public float property named "z_rotation"

  2. do in each frame:

    transform.localRotation = Quaternion.Euler(0, 0, z_rotation);

  3. animate my "z_rotation"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges