Weltall Zero for my question #2, could you explain both approaches? So I think I was going to use the first, if only so I could have a separate collider on the sword (so that i can tell when it hits something, not when the entire player sprite does), but i'm also interested in approach 2 just for future knowledge
Sure!
For the first (simpler) approach:
- If you don't have it like this already, you should have a gameObject for your character, then another childed object (e.g. right click on the character -> "create empty") that contains the sword's collider. Name this sub-object e.g. "Sword" or "Sword Collider". The name is important for reasons mentioned below, so be sure to pick a consistent, descriptive name.
- You will only have an animator on the root (the character), not on the sword. This animator controls
both your character's collider (e.g. if your character steps forward), and the sword's collider (whether it's active or not, and the size of it, if e.g. the swing has different frames with different hitboxes each).
- When you're recording animation changes on the base object (through the little red circle "record" button), Unity lets you click its sub-objects without deselecting the parent. Then you can just change the sub-object values, including their collider's position and size; these changes will be recorded in the animation of the parent object.
- An important thing to note here is that Unity records these changes referring to the
name of the sub-object. If you change the name of the sub-object, the animation will break (the values will show in yellow, saying that no such sub-object exists). This also happens if you change the type of collider (e.g. from circle to box).
The second approach is considerably more complicated, and there's probably many ways to do it. What I have is a character with an animation that has them swinging an invisible weapon, and a weapon with a swing animation. Each has its own animator, and you play both animations at the same time.
In my case, for maximum flexibility (allowing any character to use any weapon), I do several things:
- On the character I animate a "point" that marks the character's hand position (simply an empty sub-object).
- I child the weapon to the aforementioned "hand" point (the weapon's center point is its the grip / handle).
- I speed up or slow down the animation speed of the character's animator to make sure the total play time of the swing animation matches the weapon's. This allows me to have weapons that swing faster or slower.