Table of Contents

Basic 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.

Usage

There are many ways to move objects around in Unity: Rigidbodies, CharacterControllers, and direct movement to name a few. The Basic Motor does direct movement, which simply updates the AI's position and rotation every Update. It can also calculate paths using the current Navigator (likely a Basic Navigator).

It is the fastest way to move an object, but unfortunately it can't account for things like gravity, steps, and ramps. A Rigidbody will handle some of these things but may be subject to things like sliding and an inability to step.

A Character Controller Motor can handle most of these things, but will come at the cost of being more expensive to update.

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