Instantiate a object that has an animation...but animation ignores rotation...

Hello everybody,

I just started experimenting with unity a few days ago.
I am currently trying to get a top-down engine running.

This is the current situation:
http://www.it-liebig.com/unity/Link/Link.html

Walk with W,A,S,D and attack with SPACE.

As you will see, the sword that is spawned when pressing SPACE will ignore the players direction.

I created a animation for the sword…which is the one you can see…but the animation ignores the rotation I assign it.

When the player spawns the objSword its done this way:

void checkAttack()
    {
        if (!this.isAttacking)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                this.isAttacking = true;
                Invoke("stopAttack",this.attackDuration);

                if (this.weapon)
                {
                    Quaternion rotate = Quaternion.identity;

                    if ((this.direction & eDirection.down) == eDirection.down)
                        rotate = new Quaternion(0f,0f,90f,0f);
                    else if ((this.direction & eDirection.up) == eDirection.up)
                        rotate = new Quaternion(0f,0f,270f,0f);
                    else if ((this.direction & eDirection.right) == eDirection.right)
                        rotate = new Quaternion(0f,0f,180f,0f);
                    else if ((this.direction & eDirection.left) == eDirection.left)
                        rotate = new Quaternion(0f,0f,-0f,0f);

                    GameObject _sw = Instantiate(this.weapon,this.transform.position, rotate) as GameObject;
                    _sw.transform.rotation = rotate;
                    _sw.transform.animation.renderer.transform.rotation = rotate;
                }   
            }
        }
    }

I am using a bitshifting enum to support multiple directions for walking.

So, I then spawn the objSword (_sw) and try to assign the rotation…but this is ignored.
Should the sword be a child of the player object?

Did I do something horrible wrong?
I think I am missing something important here…some fundamental understanding maybe.

Thanks in advance!

You probably want to put the sword object into the center of an empty object and then rotate the empty object instead of the sword.

Thanks for the reply.

I actually tried that a couple of times… however, it will never accept the inital rotation …its always resetted.
I am quite shocked. I had a blast the last few days and got everything to work really quickly…and now I cant get a rotating object done that should be centered around another object… been stuck with this for 4 hours now.

I guess I will browse the web some more… really weird…

Have you tried something like this:

    float angle;

    if ((this.direction & eDirection.down) == eDirection.down)
        angle = 90;  
    else if ((this.direction & eDirection.up) == eDirection.up)
          angle = 270;
    else if ((this.direction & eDirection.right) == eDirection.right)
        angle = 180;
    else if ((this.direction & eDirection.left) == eDirection.left)
        angle = 0;
     GameObject _sw = Instantiate(this.weapon,this.transform.position, Quaternion.identity) as GameObject;

    _sw.transform.rotation = Quaternion.Euler(0, 0, angle);

Sorry for the late reply…got realy frustrated with this and took a small break.
Anyway, I tried this right now and it doesn’t affect the angle in any way… maybe animations can’t be done relative to a position somehow? I must really be missing something.
But, thanks again… I somehow HAVE to get this to work. If I can’t even get something like this in a sandbox to run then I should never try and make a real game :smile:

Edit
This is what the animation for the sword slash looks like

I created that animation in the Scene View and then added the objSword as a prefab.

Animations in unity are never relative, that is why you need to put the animated object in an empty child and rotate that. Then the animation rotation will happen in the local coordinate system of the child. And the rotation of the child object will influence how the child’s coordinate system is aligned. It should work that way, i do similar animations all the time. …

1 Like

Thanks for the reply …once again :slight_smile:

Once I am back home I’ll try the following:

When the Player pushes the attack key →
→ Create a GameObject
→ Assign thePlayers transform to the new GameObjects transform.parent (This is the important step, correct?)
→ Instantiate the objSword, assign the new GameObjects.transform to the objSword.transform.parent
→ Rotate the new GameObjects as needed
→ Animation should now be correct

So, in the end I do not directly touch the rotation property of the objSword at all…but I change the rotation of its parent…correct?

I haven’t gone into the details but looks like a similar issue we’d faced with
animation resetting stuff, so all we did was to put the rotation calculations
in the LateUpdate() and it worked well for us :slight_smile:

Okay…so, can you explain why?
It doesn’t really make sense to me. I could not test the lateUpdate() method yet…but I will give it a try later.

But…okay…wait…so you are starting the animation when the GameObject is instantiated…and set the rotation (from which everything should be relative) in the LateUpdate() …sounds …wierd.

Here is a small project that does things the way I suggested and it seems to work like you want it to…

1914464–123522–SwordTest.zip (1.59 MB)

1 Like

Awesome. Thank you! I’ll take a look at it in about 2 hours :slight_smile:
Thanks for your help so far. I really appreciate it.

Alright. I tested it and now it works.

So, what I did not expect was that I can not play a animation relativ to the current transform without nesting the obj that player the animation within another gameObject. Basicly, I should not try to rotate the animation (this wont work…the animation will overwrite the rotation) but rotate the GameObject that is used as a container.

Thanks a lot. This will need some getting used to… :wink:

1 Like