public static IObservable <NavMeshAgent> SetDestinationAsObservable(this NavMeshAgent agent, Vector3 position)
        {
            agent.SetDestination(position);

            var o = agent.ObserveEveryValueChanged(x => x.hasPath)
                    .SkipWhile(x => !agent.hasPath)
                    .Where(x => !x)
                    .First()
                    .Select(_ => agent);

            return(o);
        }
예제 #2
0
    public void RunAway()
    {
        currentState            = EnemyState.TENSION;
        coloring.material.color = Color.red;
        escapeTime         = 0;
        agent.angularSpeed = 200;

        //プレイヤーと逆方向を向く
        var diff = (transform.position - player.transform.position).normalized;

        diff.y             = 0;
        transform.rotation = Quaternion.FromToRotation(diff, Vector3.up);
        agent.SetDestination(GetNextPosition());

        //目的地に近づいたら次の目的地を検索
        agent.ObserveEveryValueChanged(d => agent.remainingDistance)
        .Where(d => d < 2.0f)
        .Where(_ => currentState == EnemyState.TENSION)
        .Subscribe(_ =>
        {
            var nextposition = GetNextPosition();
            agent.SetDestination(nextposition);
        }).AddTo(gameObject);

        //一定距離離れて一定時間経ったら状態を戻す
        this.UpdateAsObservable()
        .TakeWhile(_ => currentState == EnemyState.TENSION)
        .Select(_ => (transform.position - player.transform.position).sqrMagnitude)
        .Where(distance => distance > targetDistance * targetDistance)
        .Subscribe(distance =>
        {
            escapeTime += Time.deltaTime;
            if (escapeTime >= 10)
            {
                Usual();
            }
        });
    }