Example #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (target != null)
        {
            if (path == null)
            {
                return;
            }

            if (currentWaypoint >= path.vectorPath.Count)
            {
                return;
            }

            Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;

            float distance = Vector2.Distance(rb.position, (Vector2)path.vectorPath[currentWaypoint]);
            if (distance < nextWaypointDistance)
            {
                currentWaypoint++;
            }

            float distanceToTarget = Vector2.Distance(rb.position, target.position);

            if (distanceToTarget >= maxDistanceToTarget)
            {
                cm.ApplyMovement(direction);
            }
            else // to tutaj ogarnia sytuacje w ktorej obiekt znalazl sie wystarczajaco blisko celu
            {
                // zatrzymywansko
                cm.ApplyMovement(Vector3.zero);

                // detargetowansko
                target = null;

                // niszczenie znacznika jesli taki jest
                if (targetGameObject != null)
                {
                    Destroy(targetGameObject);
                    targetGameObject = null;
                }
            }
        }
    }