- Home /
How do you synchronize a sprite animation with a plane animation?
Hi,
For example lets say I have a character that is just a plane with a sprite for walking animation, now that character is holding a flash light and that flash light has to be a plane with a texture applied because I need to dim the light for game play purposes that's why I cant just paint it in my walk animation sprite.
What I need to do is pin that plane to follow the sprite animation of my character.
Maybe an image will make these clear, I was able to animate the collision plane in a 3d program, matching frame by frame the flash light to match the hand of my character. But in unity the plane animation with my sprite animation does not match in speed, my plane is a lot faster than the sprite sheet animation, I can lower the speed but it seams I'm just guessing and it never matches perfectly.
Thanks for your time.
$$anonymous$$y first guess, is that your sprite-animation is framerate-dependant. You should use Time to calculate your animation, not just a "number of frames". $$anonymous$$aybe you can post more details about how exactly you're doing the animation to help you further...
Greets
Here are some key Factors of how I'm doing the animation:
I set the Project Fixed Timestep to 0.06
The basic logic of the animation is this: I have a material with a png as texture. This png represents the spritesheet of the animation and I use the renderer.material.SetTextureOffset to move the offset over the material (based on a frame index) to animate it.
I have a variable called FrameDelay that specifies the time to wait between changing from one frame to another. This helps me to run some animations faster than others.
I have a variable called animDelay that specifies the time to wait before restarting the animation frame cycle. So the code looks more less like this.
void FixedUpdate () { //Controls time between animation cycles elapsedTime += Time.deltaTime; //Controls time between animation frames elapsedTimeFrame += Time.deltaTime; //if must start an animation cycle if (elapsedTime >= animDelay) { //If must change to the next frame if (elapsedTimeFrame >= FrameDelay || FrameDelay == 0f) {
//Calculate offset Vector3 offset = CalculateOffset(FrameIndex, TotalFrames); //$$anonymous$$ove the material offset renderer.material.SetTextureOffset("$$anonymous$$ainTex", offset); //Check if need to restart animation if (FrameIndex> TotalFrames) { //Restart Frame index FrameIndex = 0; //Restart the image offset
renderer.material.SetTextureOffset("$$anonymous$$ainTex", StartingOffset); //Restart the Time elapsedTime = 0f; } //Restart time between frames elapsedTimeFrame = 0f; } } }
I've taken a look at your code, and I think I've found your problem. It's when you reset your elapsedTime and elapsedTimeFrame to 0. You should not just set it to 0, but subtract the correct value. The fact is that for instance your elapsedTime can be much bigger than animDelay, due to a small framedrop. In that case your ti$$anonymous$$g would be all messed up.
I would suggest, when you reset your ti$$anonymous$$g, you should do elapsedTime -= animDelay
I hope this makes any sense, but I would also like to add that it's kind of a weird idea to use Time.deltaTime in a FixedUpdate().
You should either use the Update() with Time.deltaTime or the FixedUpdate() with Time.fixedDeltaTime.
I really hope this helps you on your way.
Greets,
Tim
Your answer

Follow this Question
Related Questions
2D Sprites don't match the position of the 2DColliders 0 Answers
problem with a box collider 2d. 0 Answers
Is it possible to detect which side of a sprite a touch came in from? 0 Answers
How to eliminate gaps between 2d colliders? 2 Answers
Personaje cambie de sprite al teleportarse [Characters change sprite to teleport] 0 Answers