0
\$\begingroup\$

I am working on a melee game project, and i have a gameobject which holds the weapon, and it is what moves when i swing the weapon. It has an idle 'animation' (its just one keyframe of the idle position of the weapon), and then the 4 swing directions.

I do not use the animator to handle the transitions between idle and swinging, i want to use a script for that so i can control the windup speed easier, and also incase i decide to add feints and stuff that would interrupt the windup process, but i do use animations for the swing directions. What i want to happen is the script controls moving from the idle position to the very start of the swing position, and then the swing animation plays.

However, as long as the animator is active, any attempt to move the weapon with code does not work, it follows the animator instead. I tried removing the animation from the idle state, so theoretically there would be no animation playing when the idle state is active, and i also tried removing the keyframe from the idle animation, thinking that would work, but it didnt work either.

So, how can i still move the weapon to its swing starting point using code, even if there is an animation playing? Or just override what the animation is telling it to do. I could just script the swing animations but i would rather not.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

However, as long as the animator is active, any attempt to move the weapon with code does not work, it follows the animator instead.

As you've noticed, any properties that are animated by the Animator will have their values set by the Animator every frame, overriding any changes to those properties that you make in code.

i also tried removing the keyframe from the idle animation, thinking that would work, but it didnt work either.

My experience has been that this aspect of the animator is very buggy. Sometimes, when you remove a property from all animations, the animator still overrides that property every frame. I'm not sure exactly what the cause is, but it's very difficult to fix. IIRC one fix is to recreate the animations and Animator, but this can be very tedious.

A common solution to the general problem (changes to position are overridden by the Animator) is to utilize the transform hierarchy. For example, you might make a "SwordContainer" GameObject and make the sword a child of the "SwordContainer". Then, the Animator would move the sword, while your script would move the "SwordContainer".

Another solution is to temporarily disable the Animator while you are manually moving things around with code, though it can be a headache to ensure that nothing suddenly jumps to a new position when you next turn the Animator on.


However, before you spend lots of time and effort reinventing the wheel, perhaps you should reconsider. The things that you want to do should be possible with the Animator:

i want to use a script for that so i can control the windup speed easier

You can do this with an Animator by adjusting the Speed property of the Animator

also incase i decide to add feints and stuff that would interrupt the windup process

The Animator already supports transitioning from one state to a different state before the animation finishes, and even supports interrupting a transition between states.

\$\endgroup\$
4
  • \$\begingroup\$ That first point, the "removing the animation from the state" one, i made a typo in the question, i have fixed it. \$\endgroup\$
    – Chuck
    May 23, 2023 at 23:04
  • \$\begingroup\$ While it is true that i shouldnt reinvent the wheel here, theres a reason i want to do it this way. The swing animation does not include the windup, so i would have to rely on the animator transition duration for the windup, and messing with the transition stuff gives me a headache, and my way makes more sense to my brain. There are other reasons but thats the big one. Also im not very familiar with the animator. Im much more of a programmer than i am an animator, so id rather do things with code than with the animator. \$\endgroup\$
    – Chuck
    May 23, 2023 at 23:13
  • \$\begingroup\$ @Chuck In the long run, you will be much better served by learning how to use all of the features of the Animator, rather than writing a proprietary system that does exactly the same thing. As for the windup, can you make this a separate animation? Then your state machine would transition from Idle > Windup > Swing \$\endgroup\$
    – Kevin
    May 23, 2023 at 23:50
  • \$\begingroup\$ I did find a solution that uses the animator. Sort of. It does kind of involve making a separate windup animation. i will post what i came up with in just a moment \$\endgroup\$
    – Chuck
    May 24, 2023 at 1:42
1
\$\begingroup\$

I found a solution. What i did was make an animation that has one keyframe on the start of the weapons attack position, or the end of the windup. Then what i did was make a blend tree, with the idle position in the middle of the tree, and then the new animation on the side the windup begins on.

Make a float parameter. The idle animation plays when the parameter is 0, the new animation plays when the parameter is 1. Then in the script you can use

windupLerp = Mathf.Clamp(windupLerp+Time.deltaTime,0,1);

Animator.SetFloat("parameterName", windupLerp);

Then when the windup lerp is 1, you play the swing animation. So i suppose to answer the original question, there probably isnt a way, but this is a solution to my specific problem.

Also, this solution worked really well for me because there are multiple attack directions. I can have a 2d blend tree and have windups to each direction, just by making more animations with the one keyframe at the end of the windup.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .