2
\$\begingroup\$

Been stuck on this one for a few days.

I have an axe that moves around with the mouse. When the mouse is in range of a tree, you can left click to start the axe swing animation. Once the animation is complete, it goes back to the idle state in the Animation Controller.

The problem with the current solution is the axe can clip through the geometry of the tree. I tried the 2 camera system (setting depth) to render the axe on top, but it didn't feel good.

The tree and axe has colliders on them. The axe has a rigidbody.

I then tried another idea. I disabled the animation controller for the axe on collision enter, and set the axe isKinematic to true. This worked, the axe would not clip the tree, however, once I enabled the animation controller, the axe animation would continue to playing. So I tried to switch the state back to idle and thought maybe cross fading would solve it, but it didn't work.

Is there a better way to do this?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Changing the animation state based on collision is a good way to do it. You're on the right track. Chances are, if crossfading between states didn't work, you might have set it up wrong.

Here is how I would do it:

Firstly, create different states for Idle and Swing (you've already done this). When the axe hits a tree, instead of disabling the animation controller, you simply transition out of the swing state.

void OnCollisionEnter(Collision collision) {
    // do some check to see if we've hit a tree
    ...
    // if tree
    anim.SetTrigger("HitSomething");
    ...
}

In your animator setup, create a trigger of the same key and create a transition from your swing state to your idle state.

Animator screenshot

Now the important part is setting up the crossfade between the two animation states. Your original transition from the swing state to the idle state probably has an exit time to allow for the swing to transition to idle automatically when the animation is done. We can leave that and make a new transition from swing to idle (right click swing, make transition, drag to idle).

With your new transition selected, uncheck exit time, set transition offset to 0 (immediately crossfade), and set transition duration to however long you want the length of the crossfade to be (probably 1).

Inspector screenshot

When you're done, your swing animation should stop and fade back to idle when you hit a tree. That's it!

\$\endgroup\$

You must log in to answer this question.

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