Search code examples
c#unity-game-engineanimator

If function with parameters inside Animator


enter image description hereI tried to make an if statement that goes working when a parameter inside the animator reaches a value. But I dont know how to access those parameters by script. I tried a couple of things (PlayerMovement is a public Animator):

if (PlayerMovement.Parameter("PlayerDayDreaming" = 1f))
{
Debug.Log("Switch Scene");
}

and

if(Playermovement.Parameters.Daydreaming == 1f)
{
Debug.Log("Switch Scene");
}

And a couple more look a likes but I just couldnt figure it out. Does somebody know how to do this?

Edit: I added a screenshot


Solution

  • I am unfamiliar if Animator has the field Parameters. I know it has an array of the parameters as paramaters. Generally, when trying to get a specific parameter field, you would use GetBool, GetInteger or any of the corresponding counterparts to your specific parameter field type.

    If the field Parameters exists in the version of Unity you are using, then you can keep using it. The other mistake in your code as pointed out in the comments is you are currently using the assignment operator (=) instead of the comparison operator (==) in your if conditional which should always return true as the assignment should succeed. The last issue is you are directly comparing a float, which can have issues due to floating-point precision. I used Mathf.Approximately to fix this.

    With using a parameter check based on parameter type and the fixed conditional, the code should looks something like:

    if(Mathf.Approximately(PlayerMovement.GetFloat("PlayerDayDreaming"), 1f))
    {
        Debug.Log("Switch Scene");
    }