- Home /
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
for VERY well written and detailed question. We don't get many of those around here these days
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))
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.
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)
Your answer
Follow this Question
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