Пример #1
0
    void Update()
    {
        Vector2 moveForce = new Vector2(); //0,0

        switch (movementType)
        {
        case Mode.pathFollow:
            if (path != null)
            {
                moveForce = pathFollowing();     //returns spd clamped force needed to "seek" current target
            }
            break;

        case Mode.curveFollow:
            currentPathPercent += velocity / 100 * Time.deltaTime;         //every update calculating current path percentage according to the defined speed TODO check variable use here

            Vector3[] arr = path.nodesVector3.ToArray();
            transform.position = path.NewPositionByPath(arr, currentPathPercent); //moving the 'Enemy' to the path position, calculated in method NewPositionByPath
            if (path.rotationByPath)                                              //rotating the 'Enemy' in path direction, if set 'rotationByPath'
            {
                transform.right = path.Interpolate(path.CreatePoints(arr), currentPathPercent + 0.01f) - transform.position;
                transform.Rotate(Vector3.forward * 90);
            }
            if (currentPathPercent > 1)                        //when the path is complete
            {
                if (path.loop)                                 //when loop is set, moving to the path starting point; if not, destroying or deactivating the 'Enemy'
                {
                    currentPathPercent = 0;
                }
                else
                {
                    // Destroy(gameObject); //TODO or, linger
                }
            }
            break;

        case Mode.seek:
            moveForce = seek(currTarget);
            break;

        case Mode.escape:
            moveForce = flee(currTarget);
            break;

        case Mode.pursuit:
            targetVelocity = currTarget - prevTarget;
            prevTarget     = currTarget;
            moveForce      = pursuit(currTarget, targetVelocity,
                                     dynamicT? (int)(Global.findVectorDist(currTarget, (Vector2)transform.position) / max_velocity) :
                                     constantT); //dynamic T dependent on distance between the two
            break;

        case Mode.wait:
            moveForce = Vector2.zero;
            break;
        }

        moveForce = moveForce / mass;

        if (smooth)
        {
            Vector2 steering = moveForce - currForce;                           //"currForce" is actually the force in previous run
                                                                                //maybe some truncating of the steering force / limit w max force variable here
            steering /= 5;                                                      //arbitrary; TODO max force --> how should this be determined?
            currForce = Vector3.Normalize(currForce + steering) * max_velocity; //so that overall the velo is the same
        }
        else
        {
            currForce = moveForce;
        }

        transform.position = transform.position + (Vector3)currForce;
    }