User Tools
Sidebar
Using RAIN
AI Characters
Environment
AI Rig
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.
Actions
- Memory: For storing and sharing values and objects among the different AI elements. Can be a Basic Memory or a Custom Memory.
- Mind: Handles the AI behavior. Can be a Basic Mind or a Custom Mind.
- Motion: Handles the AI movement. Can be a Basic Motor, Character Controller Motor, Mecanim Motor, or a Custom Motor.
- Animation: Handles the AI animation. Can be a Basic Animator, Mecanim Animator, or a Custom Animator.
- Navigation: Creates paths for the AI to follow. Can be a Basic Navigator or a Custom Navigator.
- Perception: Interprets the environment so that the AI can understand it. Can be a Basic Senses or a Custom Senses.
- Extension: Custom AI Elements that can be created by the developer to better integrate with the AI.
Usage
The AI Rig is the controller for all the different elements of the AI. When created through the RAIN menu it will always be located on a child object under the GameObject.
Its main job is to update its elements, which it normally does through Unity messages (Awake, Start, Update, etc). In the event that you need more control over when the AI updates you can also turn off the default updates and take it over yourself. In general though, you don't use the rig directly, but use its elements instead.
Code
Access to the AI Rig in code is no different then any MonoBehaviour.
using RAIN.Core; using UnityEngine; public class CustomAIRigComponent : MonoBehaviour { private AIRig _aiRig = null; void Awake() { _aiRig = GetComponentInChildren<AIRig>(); } }
To access the elements within an AI Rig, you have to use the AI property on the rig.
using RAIN.Core; using UnityEngine; public class CustomAIRigComponent : MonoBehaviour { private AIRig _aiRig = null; void Awake() { _aiRig = GetComponentInChildren<AIRig>(); } void Start() { // In all of these cases you can use the Property directly RAIN.Memory.BasicMemory tMemory = _aiRig.AI.WorkingMemory as RAIN.Memory.BasicMemory; RAIN.Minds.BasicMind tMind = _aiRig.AI.Mind as RAIN.Minds.BasicMind; // At least one of these would end up being null RAIN.Motion.BasicMotor tMotor = _aiRig.AI.Motor as RAIN.Motion.BasicMotor; RAIN.Motion.MecanimMotor tMecanimMotor = _aiRig.AI.Motor as RAIN.Motion.MecanimMotor; // Same here RAIN.Animation.BasicAnimator tAnimator = _aiRig.AI.Animator as RAIN.Animation.BasicAnimator; RAIN.Animation.MecanimAnimator tMecanimAnimator = _aiRig.AI.Animator as RAIN.Animation.MecanimAnimator; RAIN.Navigation.BasicNavigator tNavigator = _aiRig.AI.Navigator as RAIN.Navigation.BasicNavigator; RAIN.Perception.BasicSenses tSenses = _aiRig.AI.Senses as RAIN.Perception.BasicSenses; // And you can also access custom elements from here //_aiRig.AI.AddCustomElement() //_aiRig.AI.RemoveCustomElement() //_aiRig.AI.CustomElements } }
Here is an example of doing your own updates instead of Unity. The AI Rig already had its messages turned off in the editor ahead of time. Note that this is an advanced topic, normal use does not require relaying your own messages.
using RAIN.Core; using UnityEngine; public class CustomComponent : MonoBehaviour { private AIRig _aiRig = null; void Awake() { // We don't call anything here as the AI Rig may not be fully // initialized yet (Awake may or may not have been called) _aiRig = GetComponentInChildren<AIRig>(); } void Start() { _aiRig.AIAwake(); _aiRig.AIStart(); } void Update() { if (!_aiRig.UseFixedUpdate) _aiRig.AIUpdate(); } void LateUpdate() { _aiRig.AILateUpdate(); } void FixedUpdate() { if (_aiRig.UseFixedUpdate) _aiRig.AIUpdate(); } void OnAnimatorMove() { _aiRig.AIRootMotion(); } void OnAnimatorIK(int aLayerIndex) { _aiRig.AIIK(aLayerIndex); } }
In this case we are doing the exact same updates that the AI Rig would do on its own normally, but this was just for example.