How to get a mechanim animation state change to work?

Hi,
I am simply trying to get an animation state to change via mechanim, and I cannot get it to work. I can access the animator component, and toggle its enabled/disabled state, but when I use “setTrigger”, it doesn’t trigger the mechanim state to change.

I have a character that is Idle. There is a trigger condition “Walk” setup on the transition from Idle->Walk, but it never changes when called from my script. Am I doing something stupid? Calling “.play(“Walk”)” works, but setting the trigger does not.

`import { Animator, Behaviour, serializable, serializeObject, GameObject } from “@needle-tools/engine”;
import { Object3D } from “three”;

export class AnimatorExample extends Behaviour {

@serializable(Animator)
public personAnimator?: Animator;

start()
{
        if (this.personAnimator) {
        this.personAnimator.setTrigger("Walk");
        }

}

}`

Original Post on Discord

by user 259070246382469121

Hey :wave: Are you sure that the trigger name isn’t walk or Walk (with an extra space at the end)?

Since if play() works correctly, then indeed the only problem would be with the conditions.

Can you screenshot the transition?

Hey @kipash :cactus: Thank you for your response. Im not sure what the issue was. I checked spelling and all was correct. I deleted the animator controller and re-made it and it was fine then.

I have since modified the script and am now facing an error when trying to run .setTrigger() on multiple animators in an array. Can you please advise on why I get the error “.setTrigger is not a function”. In Unity, I have an array with 6 animators assigned. Is it something to do with how I am initially declaring the array?

`import { Animator, Behaviour, serializable, serializeObject, GameObject } from “@needle-tools/engine”;
import { Object3D } from “three”;

export class RaceStarter extends Behaviour {

@serializable(Animator[6])
public horseAnimators?: Animator[];

@serializable(Animator)
public raceAnimator?: Animator;

start()
{
        console.log("RaceStarter start");

        this.raceAnimator?.setTrigger("StartRace");

        //start all horses
        if (this.horseAnimators) {
            for (let i = 0; i < this.horseAnimators.length; i++) {
                this.horseAnimators[i].setTrigger("StartRace");
            }
        }

}

}`

by user 259070246382469121

The array should be delcared like so.

    @serializable(Animator)
    public horseAnimators?: Animator[];

You can check that the array is correct my simply logging the array variable into console.

As usual with all my questions here, I am an idiot. Thank you :slightly_smiling_face:

by user 259070246382469121

Don’t worry :green_heart:

Glad that it is working :+1: