Пример #1
0
        // In this function we actually move each assigned unit of a grid point towards that moving (in most cases) grid point
        // We use the Move function of the CharacterController and the motion is calculated based on the distance from the unit to the grid point.
        public void MoveUnitsCharacterControllerMode(int gridPointIndex, FormationGridPoint fgp, Vector3 vlcity, float endReachedDistance)
        {
            CharacterController controller = fgp.GetCharacterController();

            if (!controller)
            {
                Debug.LogError("FormationGrid.MoveUnitsCharacterControllerMode(): Character Controller missing on assigned unit.");
            }

            float distanceToGridPoint = fgp.CalculateDistanceUnitToGridPoint();

            //if (gridPointIndex == 0) DebugPanel.Log("GridPoint [" + gridPointIndex + "] unittogrid", "Grid", distanceToGridPoint);

            // default acceleration multiplier
            float acceleration = 1.0F;

            if (distanceToGridPoint > endReachedDistance * 5)
            {
                // takeover
                acceleration = accelerationStraggler;
            }
            else if ((distanceToGridPoint > endReachedDistance) && ((distanceToGridPoint < endReachedDistance * 5)))
            {
                // slowdown
                float slope     = 1 / (4 * endReachedDistance);         //      1 / ((5-1) * endReachedDistance)
                float intercept = -1 * (slope * endReachedDistance);    //

                acceleration = slope * distanceToGridPoint + intercept; //      a = 0 at endReachedDistance and a = 1 at 5*endReachedDistance

                //acceleration = distanceToGridPoint / 0.5F;
            }
            else if (distanceToGridPoint < endReachedDistance)
            {
                acceleration = 0.0f;
            }

            //if (gridPointIndex == 0) DebugPanel.Log("GridPoint [" + gridPointIndex + "] acceleration", "Grid", acceleration);

            Vector3 direction    = fgp.GetPosition() - fgp.GetAssignedUnit().transform.position;
            Vector3 fgp_velocity = direction * acceleration;


            if (useGravity)
            {
                // Use gravity and calculate vertical velocity down
                float vSpeed = fgp.GetUnitVerticalSpeed();
                if (controller.isGrounded)
                {
                    vSpeed = 0;
                }
                vSpeed -= gravity * Time.deltaTime;
                fgp.SetUnitVerticalSpeed(vSpeed);
                fgp_velocity.y = vSpeed;
            }


            controller.Move(fgp_velocity * Time.deltaTime);

            fgp.SetAssignedVelocity(fgp_velocity);
        }