News Forums RAIN General Discussion and Troubleshooting Check mecanim parameter

This topic contains 1 reply, has 2 voices, and was last updated by  prime 1 month, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #35027

    supagu
    Participant

    So I am setting some mecanimator paramters in script to control the state machine of my actor, for example “Prone” = true cause the player to go to the prone state.

    Now I dont want to have to duplicate these parameters in the AI WorkingMemory to allow the AI to also know the player has gone prone.
    Is there some way to evaluate parameters from the mecanim in the behaviour tree?

    Note that checking the the actual mecanim state is not suitable as “Prone” is made up of multiple states and transitions. eg: Prone, Crawl

    #35037

    prime
    Keymaster

    There isn’t a built in node to do it. However, you could write a custom action to do it. It would look something like this:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    using RAIN.Representation;
    using RAIN.Animation;
    using RAIN.Motion;
    [RAINAction("Check Mecanim Parameter Value")]
    public class CheckMecanimParameter : RAINAction
    {
        public Expression ParameterName = new Expression();
        public Expression ParameterValue = new Expression();
        private Animator _mecanimAnimator;
        public override void Start(RAIN.Core.AI ai)
        {
            base.Start(ai);
            if (_mecanimAnimator == null)
            { 
                if (ai.Animator is MecanimAnimator)
                    _mecanimAnimator = ((MecanimAnimator)ai.Animator).UnityAnimator;
                if ((_mecanimAnimator == null) && (ai.Motor is MecanimMotor))
                    _mecanimAnimator = ((MecanimMotor)ai.Motor).UnityAnimator;
                if (_mecanimAnimator == null)
                    _mecanimAnimator = ai.Body.GetComponentInChildren<Animator>();
            }
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            if (_mecanimAnimator == null)
                return ActionResult.FAILURE;
            if (!ParameterName.IsValid || !ParameterValue.IsValid)
                return ActionResult.FAILURE;
            string tParameter = ParameterName.Evaluate<string>(ai.DeltaTime, ai.WorkingMemory);
            bool tValue = ParameterValue.Evaluate<bool>(ai.DeltaTime, ai.WorkingMemory);
            if (_mecanimAnimator.GetBool(tParameter) == tValue)
                return ActionResult.SUCCESS;
            return ActionResult.FAILURE;
        }
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.