Example Behavior Tree in Unity Behave

Here is the Behavior Tree I used for the Lizard Enemy in the “Lizards” game demo. In some respects it’s kind of like a graphically modeled version of scripted if-then-else statements. And honestly, for this small of a project, it may have been better to use a mess of if-then-else statements. But it’s nice that there’s a bit more scalability to this approach. I can reuse these trees easily for different enemies and keep expanding the tree with more complex behaviors.

Two key elements I found very useful to have were the Check action and ExitIf Decorator. Check just returns success or failure if its given string expression is true or false. ExitIf is pretty much the same thing, but in the reverse. It returns failure if true and success if false, thus exiting out of a subtree if the condition is met. There’s a little bit of reflection magic here, but your expressions are still limited to (&&,||, and !). See the code for this below.

Lizard Root Tree:

Patrol Tree:

EvaluateExpression Snippet:

private bool EvaluateExpression( string expr ) {
    string[] exprs;
    exprs = expr.Split( new string[] { "&&"} , System.StringSplitOptions.None );
    if( exprs.Length > 1 ) {
        foreach( string e in exprs ) {
            if( !EvaluateExpression( e.Trim() ) ) {
                return false;
            }
        }
        return true;
    } else {
        exprs = expr.Split( new string[] { "||" }, System.StringSplitOptions.None );
        if( exprs.Length > 1 ) {
            foreach( string e in exprs ) {
                if( EvaluateExpression( e.Trim() ) ) {
                    return true;
                }
            }
            return false;
        } else {
            bool not = false;
            if( expr.StartsWith( "!" ) ) {
                not = true;
                expr = expr.Replace( "!" , "" );
            }
            bool b = (bool)this.GetType().GetProperty( expr ).GetValue( this  , null );
            if( not ) {
                return !b;
            } else {
                return b;
            }
        }
    }
}
Advertisements

Blender to Unity Python Export Script

Here is a small Python script that I wrote for Blender that automatically exports an FBX directly to the folder containing my Unity project. The script uses the name of the file to create an appropriate folder and file name in the Unity project and only exports two objects in the scene (Model and Rig), thus ignoring all the other junk I put in the scene file.

import bpy
import sys
import time
import mathutils

def export_current_scene_to_unity():
    namo = bpy.path.display_name_from_filepath( bpy.data.filepath )

    bpy.ops.object.mode_set(mode = 'OBJECT')
    bpy.ops.object.select_name(name='Rig')
    bpy.ops.object.select_name(name='Model',extend=True)
    bpy.ops.object.select_hierarchy(direction='CHILD',extend=True)

    bpy.ops.export_scene.fbx(filepath="/Users/carl/Unity/TheGame/Assets/Art/" + namo + "/" + namo + ".fbx", 
        check_existing=False,
        use_selection=True, 
        global_scale=1.0, 
        axis_forward='-Z', 
        axis_up='Y', 
        object_types={'ARMATURE', 'MESH', 'EMPTY'}, 
        use_mesh_modifiers=True, 
        mesh_smooth_type='OFF', 
        use_mesh_edges=False, 
        use_anim=True, 
        use_anim_action_all=True, 
        use_default_take=False,
        use_anim_optimize=True, 
        anim_optimize_precision=6.0, 
        path_mode='AUTO', 
        use_rotate_workaround=False, 
        xna_validate=False, 
        batch_mode='OFF', 
        use_batch_own_dir=True, 
        use_metadata=True)

class UnityExporter(bpy.types.Operator):
    bl_idname = "object.unity_export"
    bl_label = "Unity Export"

    def execute(self, context):
        export_current_scene_to_unity()
        print( "Export Finished!" )
        return {'FINISHED'}

def register():
    bpy.utils.register_class(UnityExporter)

def unregister():
    bpy.utils.unregister_class(UnityExporter)

if __name__ == "__main__":
    register()
Advertisements