Skip to content

Agent Behaviours: Simple Move

John Pan edited this page Sep 1, 2016 · 1 revision

Here we'll walk through how to create a movement script behaviour.

Ability is the bread and butter of agents, the equivalent to MonoBehaviour of Unity. Start by creating a script called SimpleMove and inherit from Ability:

public class SimpleMove : Ability {}

SimpleMove will have access to the following virtual functions: Setup (), Initialize (), Simulate (), Visualize (), and Deactivate () as well as several other functions.

The following code moves the agent's position towards the destination by 'interval' distance. Add it into SimpleMove:

void MoveTowards(Vector2d destination, long interval)
{
	Vector2d direction = (destination - Agent.Body.Position);
	direction.Normalize();
	Vector2d moveVector = direction * interval;
	Agent.Body.Position += moveVector;
}

Let's implement the function:

protected override void OnSimulate()
{
	long speed = FixedMath.Create(5);
	Vector2d destination = new Vector2d(10, 0);
	this.MoveTowards(destination, speed / LockstepManager.FrameRate);
}

The destination and speed are arbitrary values. Notice how for the interval parameter, we divide speed by LockstepManager.FrameRate to get how much the unit should move in a single frame.

At this point SimpleMove will work. Attach SimpleMove on your TutorialAgent and test it out. The units should run away from TestAgents towards position (100,0) now! Hit 'Playback' in the top left of the screen and make sure DeterminismTester doesn't throw any desync messages on the console.

Here's the full SimpleMove script:

using Lockstep;

public class SimpleMove : Ability
{
	void MoveTowards(Vector2d destination, long interval)
	{
		Vector2d direction = (destination - Agent.Body.Position);
		direction.Normalize();
		Vector2d moveVector = direction * interval;
		Agent.Body.Position += moveVector;
	}

	protected override void OnSimulate()
	{
		long speed = FixedMath.Create(5);
		Vector2d destination = new Vector2d(100, 0);
		this.MoveTowards(destination, speed / LockstepManager.FrameRate);
	}
}
Clone this wiki locally