Navmesh example. Scripting

@Lubos. HEEEEELP!

I don’t get how the animation depends on NavAgent in navmesh.blend. (character.hx) I mean pause during stop and walk animation during path following? Script looks at distance to destination. But how the distance changes within after click?
Why is character trait attached to mesh and not to armature?
Basically which methods exist for traits/objects to interact?

Ah, Character trait needs more love, it’s still very basic. Yup - script looks at the distance character moved since last frame. If character starts to move, animation is played. If character did not travel any distance since the last frame, animation is paused.

Using NavigateTree logic nodes, clicking a mouse button picks destination from navmesh. Go To Location takes this destination and starts moving object transform toward the destination. Character trait detects that transform is moving and plays/pauses the animation accordingly.

Not sure if this is clearer now, will try to cleanup the Character trait and make it a bit more robust. I am changing the trait so it can be attached to the armature directly like you expected, much cleaner that way. :slight_smile:

1 Like

How?! Which line in character.hx? i don’t know which art of exchange bewegen traits is basically possible. Properties? :slight_smile:

Hi @SpryngariaTeam,

I commented Character.hx for you to clarify how it works.

package arm;

import iron.object.Object;
import iron.Trait;
import iron.system.Input;
import iron.math.Vec4;
import iron.math.Quat;

class Character extends Trait {

	var speed = 0.0;
	var actionIdle:String;
	var actionMove:String;

	var loc:Vec4 = new Vec4();
	var lastLoc:Vec4 = null;
	var state = 0;

	public function new(actionIdle:String, actionMove:String) {
		super();

		this.actionIdle = actionIdle;
		this.actionMove = actionMove;

		notifyOnInit(init);
		notifyOnUpdate(update);
	}

	function init() {
		object.animation.pause();
	}

	function update() {
		// get current position
		var tr = object.transform;
		loc.set(tr.worldx(), tr.worldy(), tr.worldz());

		// set previous position to current position if there is no previous position
		if (lastLoc == null) lastLoc = new Vec4(loc.x, loc.y, loc.z);

		// check if character moved compared from last position
		speed = Vec4.distance3d(loc, lastLoc);

		// update previous position to current position
		// in preparation for next check
		lastLoc.setFrom(loc);

		// if state is zero (idle) and speed is greater than zero, play move walk animation
		if (state == 0 && speed > 0) {
			state = 1;
			object.animation.play(actionMove);
		}

		// otherwise if state is one (walking) and speed equals zero, pause the walk animation
		else if (state == 1 && speed == 0) {
			state = 0;
			object.animation.pause();
			// object.animation.play(actionIdle);
		}
	}
}

BR,
guzzard

3 Likes

Oh my…
Of course. I’ve searched for any ineraction between NavAgent and charakter to detect any movements.
Man, i’m dumb…
Thanks a lot.

Merged the commented version on git.

Need to get these support classes clearer as it’s often a good base for forking and adding more stuff on top. Also using this approach - even if a box or any physics object pushes into the character, it will start the walk animation. :slight_smile:

Can Armory handle crowd navmesh ?

Not yet but more navmesh features on the way: https://github.com/armory3d/armory/issues/243

1 Like