🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

In Unity, what is the best way to play two animations, from two different 3d models, at the same time?

Started by
1 comment, last by lougv22 1 week, 5 days ago

Fellow game developers,

I am working on an indie game in Unity and I have the following question. I have two characters, let's call them Character 1 and Character 2, that are facing each other and fighting. Each character is a 3d model in its own game object, with its own animation controller, and a number of animations. What I'd like to accomplish is the following: 1-2 frames after Character 1 starts performing an animation, Character 2 should perform an animation of its own and both animations should be playing at the same time. The timing of the animations for both characters is managed by code, i.e. player input is not relevant as to when an animation is performed.

This can occur anywhere between 1 and x, let's 1-5, times. So for example, Character 1 can perform 5 animations and for each of them, I'd like Character 2 to perform one of its animations 1-2 frames after the Character 1 animation has started playing.

What would be the best way to accomplish that?

Advertisement

lougv22 said:
Fellow game developers, I am working on an indie game in Unity and I have the following question. I have two characters, let's call them Character 1 and Character 2, that are facing each other and fighting. Each character is a 3d model in its own game object, with its own animation controller, and a number of animations. What I'd like to accomplish is the following: 1-2 frames after Character 1 starts performing an animation, Character 2 should perform an animation of its own and both animations should be playing at the same time. The timing of the animations for both characters is managed by code, i.e. player input is not relevant as to when an animation is performed. This can occur anywhere between 1 and x, let's 1-5, times. So for example, Character 1 can perform 5 animations and for each of them, I'd like Character 2 to perform one of its animations 1-2 frames after the Character 1 animation has started playing. What would be the best way to accomplish that?

Hello,

There are two main approaches to achieve what you're describing:

1. Using Coroutines:

This approach involves using coroutines to manage the timing of the animations. Here's how it works:

Define Coroutines: In your script, define two coroutines: one for each character.

Start Character 1 Animation: In the Character 1 coroutine, start the desired animation.

Wait for Delay: After starting the animation, use yield return new WaitForSeconds(delay) to wait for the desired delay (1-2 frames in your case).

Start Character 2 Animation: Once the delay is over, start the corresponding animation for Character 2.

Here's an example:

Code snippet

IEnumerator Character1AnimationCoroutine(Animator animator1, Animator animator2, int animationIndex)

{

animator1.Play(animationIndex);

yield return new WaitForSeconds(1f); // Adjust delay as needed

animator2.Play(GetCorrespondingAnimation(animationIndex));

}

IEnumerator Character2AnimationCoroutine(Animator animator2, int animationIndex)

{

// Wait for Character 1 animation to start

yield return new WaitForSeconds(1f); // Adjust delay as needed

animator2.Play(GetCorrespondingAnimation(animationIndex));

}

Use code with caution.

content_copy

2. Using Animation Events:

This approach uses animation events to trigger the second animation. Here's how it works:

Set up Animation Events: In the Character 1 animation, add an animation event at the desired point (after 1-2 frames).

Trigger Character 2 Animation: In the animation event script, call a function that starts the corresponding animation for Character 2.

Here's an example:

Code snippet

public class Character1AnimationEvent : MonoBehaviour

{

public Animator animator2;

public int animationIndex;

void OnAnimationEvent()

{

animator2.Play(GetCorrespondingAnimation(animationIndex));

}

}

Use code with caution.

content_copy

Choosing the Best Approach:

Coroutines: This approach is more flexible as you can easily adjust the delay between animations. However, it requires more code and can be less intuitive.

Animation Events: This approach is simpler to set up and more intuitive. However, it's less flexible as you can only trigger the second animation at specific points in the first animation.

@dennisleon Thanks for this! I went with approach #2, i.e. animation events, and that seems to work well.

Advertisement