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
2
Question by NoSoup4you · Aug 25, 2013 at 01:33 AM · camerarotationspriterendering

2D sprites with 3D camera - Change sprite upon rotation - Strange "jitter"

THE SETUP: I'm using 2D Toolkit to blend isometric sprites with a downward-angled perspective camera. You can rotate in 45 degree increments around the character, and as you do so the sprite will change to reflect the angle relative to the direction of the camera.

THE PROBLEM: At the exact midpoint of each 45 degree rotation, there is a frame where it seems to "jitter" and draw both the old and new sprite at the same time. This isn't actually possible in the code I think, as I used else-ifs and made sure there's no overlaps in the range of degrees that choose which clip to display. I suspect it's some sort of internal rendering issue, which I'm not very familiar with.

Here's what I believe to be the relevant code, in the "animation manager" component of the character:

 void Update ()
         {
         transform.LookAt(transform.position + MainCamera.transform.rotation * Vector3.forward, MainCamera.transform.rotation * Vector3.up); 
         
         setAction();
         setAbsoluteFacing();
         setRelativeFacing();
         animLib.Play(MAP[action + ((absoluteFacing - relativeFacing) + 360) % 360]);
                     //I use a map object linking strings to animation clips, and put the string together piece by piece with those three methods. My animation clips have names like Stand45, Walk180, etc.
         }
 
 void setAbsoluteFacing()
         {
         Vector3 dir = movement.getMoveDirection();
         dir.y = 0;
         if (!dir.Equals(Vector3.zero)) //Don't change direction if not moving.
             {
             float angle = Quaternion.LookRotation(dir).eulerAngles.y;
         
             if (angle >= 337.5 || angle < 22.5)        //Snap it to the nearest 45 degree increment.
                 {absoluteFacing = 0;}
             else if (angle >= 22.5 && angle < 67.5)
                 {absoluteFacing = 45;}
             else if (angle >= 67.5 && angle < 112.5)
                 {absoluteFacing = 90;}
             else if (angle >= 112.5 && angle < 157.5)
                 {absoluteFacing = 135;}
             else if (angle >= 157.5 && angle < 202.5)
                 {absoluteFacing = 180;}
             else if (angle >= 202.5 && angle < 247.5)
                 {absoluteFacing = 225;}
             else if (angle >= 247.5 && angle < 292.5)
                 {absoluteFacing = 270;}
             else if (angle >= 292.5 && angle < 337.5)
                 {absoluteFacing = 315;}
             }
         }
 
 void setRelativeFacing()
         {
         float angle = MainCamera.transform.eulerAngles.y;
         
         if (angle >= 337.5 || angle < 22.5)        //Snap it to the nearest 45 degree increment.
             {relativeFacing = 0;}
         else if (angle >= 22.5 && angle < 67.5)        //Wish I could condense to 22.5 < angle < 67.5. Any way in C#?
             {relativeFacing = 45;}
         else if (angle >= 67.5 && angle < 112.5)
             {relativeFacing = 90;}
         else if (angle >= 112.5 && angle < 157.5)
             {relativeFacing = 135;}
         else if (angle >= 157.5 && angle < 202.5)
             {relativeFacing = 180;}
         else if (angle >= 202.5 && angle < 247.5)
             {relativeFacing = 225;}
         else if (angle >= 247.5 && angle < 292.5)
             {relativeFacing = 270;}
         else if (angle >= 292.5 && angle < 337.5)
             {relativeFacing = 315;}
         }

The code's getting a little long to post, but I can post the camera stuff too if anyone wants. I have captured a small video with Fraps to illustrate the problem. Interestingly, it only showed the problem sometimes (at 0:13, 0:18, 0:23, 0:26,) but I can see it every time on my computer. Probably due to the 30fps limit on Fraps I suppose. I should mention that it only happens when I'm walking. The standing animation works smoothly.

https://dl.dropboxusercontent.com/u/6097696/Unity%202013-08-24%2019-36-17-31.wmv

Comment
Add comment · Show 4
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 Benproductions1 · Aug 25, 2013 at 01:34 AM 0
Share
  • for VERY well written and detailed question. We don't get many of those around here these days

avatar image Benproductions1 · Aug 25, 2013 at 01:38 AM 1
Share

To quicken up:

 if (a < c && c < b)

I suggest you write a function:

 bool InRange(float value, float $$anonymous$$, float max) {
     return ($$anonymous$$ < value && value < max);
 }

Then you simply write:

 if (InRange(c, a, b))
avatar image NoSoup4you · Aug 25, 2013 at 01:44 AM 0
Share

Really? I was afraid of being long-winded/noobish. I'm not all that experienced with Unity, but I try to appear competent lol. Thanks.

Re: The InRange function... I could do that, but it seems less readable and only saves a few characters of screen space. I guess I can't really win there.

avatar image Benproductions1 · Aug 25, 2013 at 01:58 AM 0
Share

Yea, sadly there is no shorthand for that. That's one of the reasons I like non-typed languages, because you can do stuff like:

 a = b = $$anonymous$$athf.Sin(j);

or

 if (a == b < c < j)

0 Replies

· Add your reply
  • Sort: 

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

16 People are following this question.

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

Related Questions

perpendicular sprites towards camera 1 Answer

Sprite facing multiple cameras? 1 Answer

2D Sprite rotation but still face the camera? 0 Answers

Synchronising the rotation around Y axis of two objects 1 Answer

Sprites is jaggy on low move speed 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