User Tools

Site Tools


rainelements:mecanimmotor

Mecanim Motor

Properties

  • Use Unity Messages (Advanced): Whether the AI is initialized and updated by Unity or done manually.
  • Use Fixed Update (Advanced): Whether the AI responds to Unity Update or FixedUpdate messages. Only valid if Use Unity Messages is checked.
  • Is Active (Advanced): Whether the AI is currently updating.
  • Body: The GameObject that the AI will move and animate.
  • Speed: The default speed (in meters per second) to use when this AI moves.
  • Rotation Speed: The default rotation speed (in degrees per second) to use when the AI turns.
  • Close Enough Distance: When the AI is within this distance to a target we consider it finished moving.
  • Close Enough Angle: When the AI is within this degree to a target we consider it finished turning.
  • Face Before Move Angle: When moving to a target this is how close we have to be to facing the target before we will move.
  • Step Up Height: Mainly used by the Basic Navigator to determine whether or not the AI is on a navigation graph. If the AI is below a graph by more than the Step Up Height it does not use that graph for the current path.
  • Allow 3DMovement: Whether or not to allow movement in 3D. If checked, the AI will no longer use the Basic Navigator to path.
  • Allow 3DRotation: Whether or not to allow rotation in 3D. If used in tandem with Allow 3DMovement it will allow an AI to point in the direction it is heading even if it is above it.
  • Valid Path Required: Whether or not the AI requires a valid path from the Basic Navigator.
  • Use Root Motion: Whether or not to use root motion to move the AI.
  • Override Root Rotation: Whether RAIN is allowed to override the root motion rotation with its own calculations.

Actions

  • Add Parameter: Allows you to forward one or more preset fields to the current Mecanim Animator Component.
    • Speed: The current velocity of the AI (any direction).
    • Rotation Speed: The current rotation speed of the AI (any direction).
    • Turn Angle: The current angle between the AI's current direction and the direction it is turning to.
    • VelocityX: The AI's current local velocity in the X direction.
    • VelocityY: The AI's current local velocity in the Y direction.
    • VelocityZ: The AI's current local velocity in the Z direction.
    • RotationX: The AI's current roation around the X axis.
    • RotationY: The AI's current roation around the Y axis.
    • RotationZ: The AI's current roation around the Z axis.

Usage

The main use of the Mecanim Motor is to allow for animation driven movement (root motion). Once you are using root motion, speed and rotation can no longer be controlled by the AI directly and instead have to be forwarded to the AnimatorController from the motor.

Even if you aren't using root motion, it is generally a good idea to design your locomotion within your Animator Controller. You can then forward values like Speed and Turn Angle to change what animations are playing.

  • Damp Time: Smooths any change to a parameter by this much time, particularly useful when used with blend trees.

Since the AnimatorController is handling speed and rotation, it can make it difficult for the AI to reach spots exactly, so it is usually wise to set a larger close enough distance and angle. Rotation can be particularly hard as you may need your AI to make fairly arbitrary rotations to get to certain targets, so the Mecanim Motor offers the Override Root Rotation attribute.

Code

Within custom actions it is possible to move the AI using the Motor property (which can be a Basic Motor, Character Controller Motor, Mecanim Motor, or Custom Motor). The Move node does this and has even more features, so there is no need to actually do this in a custom action, unless you need to work outside of the Move node.

using RAIN.Action;
using RAIN.Core;
using UnityEngine;
[RAINAction]
public class MoveSomewhere : RAINAction
{
    public MoveSomewhere()
    {
        actionName = "MoveSomewhere";
    }
    public override ActionResult Execute(AI ai)
    {
        ai.Motor.MoveTarget.VectorTarget = ai.WorkingMemory.GetItem<Vector3>("MyMoveTarget");
        ai.Motor.FaceTarget.VectorTarget = ai.WorkingMemory.GetItem<Vector3>("MyFaceTarget");
        ai.Motor.Move();
        ai.Motor.Face();
        return ActionResult.RUNNING;
    }
}

You can also do something similar outside of the behavior tree altogether. Again, the Move node is much faster to setup, and already does what this example shows. The only reason to do this would be if you need to work around the Basic Mind, and we have a better way to do that through a Custom Mind.

using RAIN.Core;
using UnityEngine;
public class ManualMove : MonoBehaviour
{
    [SerializeField]
    private Vector3 _destination;
    private AIRig _aiRig = null;
    void Start()
    {
        _aiRig = GetComponentInChildren<AIRig>();
    }
    void Update()
    {
        // This updates the AI so that it knows where the body is
        _aiRig.AI.Motor.UpdateMotionTransforms();
        _aiRig.AI.Motor.MoveTarget.VectorTarget = _destination;
        _aiRig.AI.Motor.Move();
        // This applies the movement that the motor just did
        _aiRig.AI.Motor.ApplyMotionTransforms();
    }
}

See Also

rainelements/mecanimmotor.txt · Last modified: 2022/08/13 18:13 (external edit)