User Tools
Sidebar
Using RAIN
AI Characters
Environment
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.
- Motor: The type of motor used. Currently RAIN supports a Basic Motor, Character Controller Motor, or Mecanim Motor. If you create your own Custom Motor you can select it here.
- 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.
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(); } }