本文整理汇总了C#中IState.ChangeState方法的典型用法代码示例。如果您正苦于以下问题:C# IState.ChangeState方法的具体用法?C# IState.ChangeState怎么用?C# IState.ChangeState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IState
的用法示例。
在下文中一共展示了IState.ChangeState方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
// Use this for initialization
void Start()
{
rootState = new StateMachineBuilder()
// First, approach the goal
.State<MovingState>("Approach")
.Enter(state =>
{
Debug.Log("Entering Approach state");
})
// Move towards the goal
.Update((state, deltaTime) =>
{
var directionToTarget = transform.position - goal.position;
directionToTarget.y = 0; // Only calculate movement on a 2d plane
directionToTarget.Normalize();
transform.position -= directionToTarget * deltaTime * state.movementSpeed;
})
// Once the TargetReached event is triggered, retreat away again
.Event("TargetReached", state =>
{
state.PushState("Retreat");
})
.Exit(state =>
{
Debug.Log("Exiting Approach state");
})
// Retreating state
.State<RetreatingState>("Retreat")
// Set a new destination
.Enter(state =>
{
Debug.Log("Entering Retreat state");
// Work out a new target, away from the goal
var direction = new Vector3(Random.value, 0f, Random.value);
direction.Normalize();
state.direction = direction;
})
// Move towards the new destination
.Update((state, deltaTime) =>
{
transform.position -= state.direction * deltaTime * state.movementSpeed;
})
// If we go further away from the original target than the reset distance, exit and
// go back to the previous state
.Condition(() =>
{
return Vector3.Distance(transform.position, goal.position) >= resetDistance;
},
state =>
{
state.Parent.PopState();
})
.Exit(state =>
{
Debug.Log("Exiting Retreat state");
})
.End()
.End()
.Build();
rootState.ChangeState("Approach");
}