Page 1 of 1

Change camera angle based on Animator state

Posted: Wed Sep 25, 2019 2:25 pm
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.

Re: Change camera angle based on Animator state

Posted: Wed Sep 25, 2019 3:11 pm
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.

Re: Change camera angle based on Animator state

Posted: Thu Sep 26, 2019 2:50 pm
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;

Re: Change camera angle based on Animator state

Posted: Thu Sep 26, 2019 3:07 pm
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.