How to have an NPC character auto-orient forwards (if it needs to) between two animations

I have some characters who are dancing using Mixamo dance animations. They are not controlled by the player but they do have triggers on them that are fired at certain points in music they are dancing to.

So I trigger ‘startMainDance’ for example and all the dancers start their funky grooves (which are different) and then I trigger ‘endDancing’ where two of the characters, that have been doing different dances do a backflip.

However what I need them to do when ‘endDancing’ is triggered is IF they are not facing forwards, play the appropriate turnleft/turnright animation until they are facing forward, and I could maybe give them 1.5secs or so to sort themselves out and then continue to do their backflips once they are facing forwards.

While this animation sequence isn’t going to change actually once its finalized, I was just hoping there was some ‘clever’ way of doing this dynamically so as I am building the sequence I can swap out different dance animations and not screw up the ending- it’d also be good to know for future stuff.

Anyone any ideas?

What about target matching? You might even skip the turnleft/turnright animations and let them rotate gradually as they dance, which is pretty normal in real life, too.

You could have them both target match toward a central point. Set up a central empty GameObject with two offset empty children that they can match to, so they don’t collide with each other.

Or, if you want one dancer to follow the other’s lead, add an offset empty child to the lead dancer, and make the follower target match to it.

You could trigger the target matching shortly before ‘endDancing’, so by the time you hit ‘endDancing’ the dancers should be in position.

Or you could compare vectors against a certain angle to see if you should turn:

// turning is true if the angle is > 30, false otherwise:
turning = Vector3.Angle(targetDir, transform.forward) > 30;

In that example you get targetDir by finding the direction towards the target position. You can do this with:

targetDir = lookTarget.position - transform.position;

However that will only tell you if you need to turn, not which direction is shorter. If you compared Quaternions you could find the shorter direction to turn, but that’s a little more tedious :stuck_out_tongue:

Hey guys, thanks for your thoughts. I’ll definitely try your ideas out… Right now I have a somewhat hacky fix, by briefly fading the character to some other animation that also includes position/rotation data for the wrapping container that sets it back to zeros. I’m not very happy with the result though and look forwards to trying out what you suggest and reporting back…:slight_smile: