Change camera angle based on Animator state

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
fanaei
Posts: 73
Joined: Wed Aug 15, 2018 10:38 am

Change camera angle based on Animator state

Post by fanaei »

Hi Tony
I'm trying to change camera angle based on Animator state. For example having a zoomed angle when Player plays angry animation and etc...
I tried to edit "DefaultCameraAngle" script by adding these codes:

Code: Select all

void OnConversationLine(Subtitle subtitle)
        {
            if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(GetComponent<Animator>().GetLayerIndex("Dialogue")).IsName("NoDialogue"))
            {
                cameraAngle = "full";
            }
            if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(GetComponent<Animator>().GetLayerIndex("Dialogue")).IsName("sad"))
            {
                cameraAngle = "medium";
            }
            if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(GetComponent<Animator>().GetLayerIndex("Dialogue")).IsName("angry"))
            {
                cameraAngle = "close";
            }
        }
These are just some examples. and I tried them in Update too, but in both voids, they change late and camera gets the previous value.
So I wanted to know if there is a way to change Default camera angle value before sequencer run it.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change camera angle based on Animator state

Post by Tony Li »

The Dialogue System calls OnConversationLine() before playing the dialogue entry node's Sequence. If you're changing animator states in the Sequence, then the code above won't work. Your code above will work if the animator is already in the state before OnConversationLine() is called.

What is controlling the animation?

Keep in mind that, because of transition times, it can take several frames for Animator.GetCurrentAnimatorStateInfo to report that it's in the new state.

Here's an idea: Use animation events. You don't need to modify the DefaultCameraAngle script. Instead, add a new script like this:

Code: Select all

public class CameraAngleAnimationEvents : MonoBehaviour
{
    public void SetDefaultAngle(string angle)
    {
        GetComponent<DefaultCameraAngle>().cameraAngle = angle;
    }
}
Then add an animation event to each animation clip that calls SetDefaultAngle("xxx") where xxx is the camera angle to use with the animation state. It will be used the next time Camera(default) is called.
fanaei
Posts: 73
Joined: Wed Aug 15, 2018 10:38 am

Re: Change camera angle based on Animator state

Post by fanaei »

It works but if I use it with a camera delay, like this:

Code: Select all

AnimatorPlay(think,,0.5,);
Camera(default)@0.1;
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Change camera angle based on Animator state

Post by Tony Li »

That's a good idea. Otherwise both commands will run at the same time, which means the value of "default" in Camera(default) will still be the old value.

Another idea, instead of using animation events, is to write a custom sequencer command to replace AnimatorPlay(). You can use the code from AnimatorPlay() but also set the value in DefaultCameraAngle.
Post Reply