Using the same animation for multiple gameobjetcs

Hi people,

I’m back with a simple question( I think ) yet again. I have a plane sprite that banks when going left or right. I also created 3 separate fire positions(They are children of the player gameObject, but not bundled together as children of let’s say FirePoints ) that instantiate bullets when the fire button is pressed. I also created a muzzle flash animation with 4 frames and when the fire button pressed the muzzle flash animation kicks in.

ezgif.com-gif-maker(3)

My problem now is that I can not find an easy way to change the position of the flash animation for the bank/tilt motion. I am using one animation (just changing sprites) shared by three game objects with sprite Renderers which are children of the fire positions. When I add new transform information matching the banking when the recording starts, all the positions are changing and matching the one I changed since the animation is shared by all three game objects(I think).
So should I maybe create new game objects aligning with the banking motions and add the sprites with animation(without any transform info) under them? Or should I create 3 different animation cycles?

I still think there should be an easy way or method that I am missing or over-looking and I wanted to ask for opinions…=)
Thnx in advance…Cheers…=)

1 Like

Hi,

First of all, good job on challenging yourself. The gif looks already really great! :slight_smile:

Regarding your question, I don’t know what exactly you did but when animating something, there is a simple rule to follow: To prevent conflicts between the code and the animation, do not animate an object that gets moved via code. Instead, move the visual part to a child game object and animate the child whilst moving the parent game object via code. Maybe this will also fix your problem. If not, please let me know and elaborate a bit further on what you did in Unity. Screenshots would be helpful.

Hey,

Thanx for the kind words…=)

I created 3 empty game objects as children of my player game object and called them fire position, fire position2, fire position3, and I use the transform information of these to instantiate the bullets. I created a child game object inside the first fire position, animated it, and then duplicated it twice and added the duplicated ones to the other fire positions. Now I have three game-objetcs, each has a child with a sprite renderer but all share the same animation. I struggled a while but when I created an animation only with sprite as its property I achieved the animation that you can see in the gif. (I created 3 different animators in the script for the muzzle flash animation)

I thought that I would create 2 more animations for left bank and right bank motions. And then I would call them through the player script like this:

Now my problem is: When I try to replace the position of the flash animation inside the recording by adding a transform value for one position, the other 2 are also changing since they are using the same animation(I guess) and I end up with three animations, one playing where it should play but the other two are playing way off. I can think of two solutions but they don’t feel that correct and I’m not sure if they would work.
The first is to create 4 new game objects each with a child game object with a sprite renderer. Fire Position 2-3 for no bank motion, 4-5 for left-bank motion, 7-8 for right bank motion.Then I would animate and place them correctly and activate the correct ones when bank-left,bank-right, or no-bank and deactivate the rest.

The second one is to not share one animation between the 3 muzzle flash game objects but let them have their own animation. So MuzzleFlash game object would have 4(Not firing, firing, firing when going left, firing when going right) animations as MuzzleFlash2 and MuzzleFlash3 and I would activate all there at the same time through code.

But both of these Ideas feel kind of wrong and it feels like I’m missing some information or over-looking somthing.

If the visual part of the 3 muzzle flashes is the same, you could reuse the animation for them. However, move that visual part to a child game object. Move and rotate the parent via code.

Of course, if you modify the animation itself, all game objects that are animated by that animation are affected by the changes.

After reading you suggestion I decided to change the position of the fire position Game Objects(Because I thought that they should also move a little with the movement of the ship) in a very barbaric way…=)

private void MovementAnimations()
{
if (Input.GetKeyDown(KeyCode.D))
{
animatorPlayerShip.SetFloat(“Pan”, 0.6f);

        animatorPropultion.SetFloat("ProRight", 0.6f);
        animatorGölge.SetFloat("ShadowPan", 0.6f);


        if (pUpMan.mGunOne == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x, f1.transform.position.y);
        }
        if (pUpMan.mGunTwo == true)
        {
            f3.transform.position = new Vector2(f3.transform.position.x - 0.144f, f3.transform.position.y + 0.05f);
            f2.transform.position = new Vector2(f2.transform.position.x + 0.150f, f2.transform.position.y - 0.244f);
        }
        if (pUpMan.mGunThree == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x, f1.transform.position.y);
            f3.transform.position = new Vector2(f3.transform.position.x + 0.14f, f3.transform.position.y + 0.15f);
            f2.transform.position = new Vector2(f2.transform.position.x - 0.15f, f2.transform.position.y - 0.05f);
        }

    }
    else if (Input.GetKeyUp(KeyCode.D))
    {
        animatorPlayerShip.SetFloat("Pan", 0.4f);
        
        animatorPropultion.SetFloat("ProRight", 0.4f);
        animatorGölge.SetFloat("ShadowPan", 0.4f);


        if (pUpMan.mGunOne == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x, f1.transform.position.y);
        }
        if (pUpMan.mGunTwo == true)
        {
            f3.transform.position = new Vector2(f3.transform.position.x + 0.244f, f3.transform.position.y - 0.15f);
            f2.transform.position = new Vector2(f2.transform.position.x - 0.50f, f2.transform.position.y + 0.244f);
        }
        if (pUpMan.mGunThree == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x, f1.transform.position.y);
            f3.transform.position = new Vector2(f3.transform.position.x - 0.14f, f3.transform.position.y - 0.15f);
            f2.transform.position = new Vector2(f2.transform.position.x + 0.15f, f2.transform.position.y + 0.05f);
        }


    }

    if (Input.GetKeyDown(KeyCode.A))
    {
        animatorPlayerShip.SetFloat("Tilt", 0.6f);
        
        animatorPropultion.SetFloat("ProLeft", 0.6f);
        animatorGölge.SetFloat("ShadowTilt", 0.6f);


        if (pUpMan.mGunOne == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x , f1.transform.position.y);
        }
        if (pUpMan.mGunTwo == true)
        {
            f3.transform.position = new Vector2(f3.transform.position.x + 0.144f, f3.transform.position.y - 0.05f);
            f2.transform.position = new Vector2(f2.transform.position.x - 0.150f, f2.transform.position.y + 0.244f);
        }
        if (pUpMan.mGunThree == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x  , f1.transform.position.y);
            f3.transform.position = new Vector2(f3.transform.position.x + 0.144f, f3.transform.position.y - 0.05f);
            f2.transform.position = new Vector2(f2.transform.position.x - 0.150f, f2.transform.position.y + 0.244f);
        }

    }
    else if (Input.GetKeyUp(KeyCode.A))
    {
        animatorPlayerShip.SetFloat("Tilt", 0.4f);
        
        animatorPropultion.SetFloat("ProLeft", 0.4f);
        animatorGölge.SetFloat("ShadowTilt", 0.4f);

        if (pUpMan.mGunOne == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x , f1.transform.position.y);
        }
        if (pUpMan.mGunTwo == true)
        {
            f3.transform.position = new Vector2(f3.transform.position.x - 0.144f, f3.transform.position.y + 0.05f);
            f2.transform.position = new Vector2(f2.transform.position.x + 0.150f, f2.transform.position.y - 0.244f);
        }
        if (pUpMan.mGunThree == true)
        {
            f1.transform.position = new Vector2(f1.transform.position.x , f1.transform.position.y);
            f3.transform.position = new Vector2(f3.transform.position.x - 0.144f, f3.transform.position.y + 0.05f);
            f2.transform.position = new Vector2(f2.transform.position.x + 0.150f, f2.transform.position.y - 0.244f);
        }

And this is how it looks now.
ezgif.com-gif-maker(4)

Good job! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms