1
\$\begingroup\$

I'm sure this is a common problem people face but I can't find much on this topic after some googling.

I have a sword, and a player, both which are sprites. I want the sword to be attached to the player -- Normally you would be able to just child the sword sprite to the player sprite and change the relative position of the sword sprite -- but my player's hands move during the walking animation.

So is there an easy way of moving the position of the sword sprite during each frame of the player walking animation? Or some way of attaching the handle of the sword sprite to the player's hand?

\$\endgroup\$
  • \$\begingroup\$ You could also create an animation for your sword that animates depending on the state your player is in \$\endgroup\$ – Savlon Jun 7 '15 at 9:16
  • \$\begingroup\$ yeah, this is what I ended up doing. Not sure if it's the best idea but it's working so that's a start. \$\endgroup\$ – Jeff Burghess Jun 7 '15 at 19:03
1
\$\begingroup\$

You could try making the sword and the hand same object so when you animate the hand the sword will be animated too.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

You could modify your character sprite so that the arm is separate and moves on its own as your character moves.

Then you could just attach the arm to the character, add in some code to handle making it wag around when you walk, and now you simply child the sword off of the arm and it will be moved around when the arm moves.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ Unity has some actual nice examples of this with their own 2D tutorial. \$\endgroup\$ – ProtoJazz Nov 5 '15 at 15:41
  • \$\begingroup\$ @ProtoJazz That must be where I learned that! I remember long ago doing something of that nature to have a gun move with my character's arm movements. \$\endgroup\$ – Amagii Discordus Penndragon Nov 5 '15 at 16:32
  • \$\begingroup\$ They seem to have removed it though, it was the one that came out with their 2d tools. The one with the bean looking dude with a rocket launcher \$\endgroup\$ – ProtoJazz Nov 5 '15 at 16:44
0
\$\begingroup\$

Unfortunately theres no easy built in solution to do this. What you need is a series of reference points that your sword object can position and align itself with according to what frame of the animation your character is on.

What I did personally in such situations was to open the sprite sheet up and get the pixel location of each frame where I wanted and attached object to be and write this out into an XML doc. Then using that pixel offset you can calculate where your object should be in relation to the parent at each frame of the animation. If you have a large number of these to do it would be well worth your time to find a third party app or write a utility that can expedite outputting the coordinates.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.