Exemple #1
0
    private void HandleMovingInput(object sender, EventArgs e)
    {
        // AI obstacles avoiding (nikita)

        float      maxDistance = 100f;
        RaycastHit hit;
        bool       isLeftHit         = false;
        bool       isRightHit        = false;
        bool       isObstacleOnMyWay = false;
        bool       isEnemyOnForward  = false;
        bool       isEnemyOnLeft     = false;
        bool       isEnemyOnRight    = false;

        //вводим слои для обнаружения земли и для обнаружения подбираемых\уклоняемых сущностей на трассе
        LayerMask TrackEntityMask = LayerMask.GetMask("AICastedEntity");
        LayerMask groundMask      = LayerMask.GetMask("Ground");
        LayerMask enemyMask       = LayerMask.GetMask("Ship");

        if (pilotModelView.ChechpointTarget != null)
        {
            // нормализуем вектор направления до чекпоинта, отсекаем ему Y составляющую
            Vector3 checkpointDirection = new Vector3((pilotModelView.ChechpointTarget - pilotModelView.transform.position).normalized.x, 0,
                                                      (pilotModelView.ChechpointTarget - pilotModelView.transform.position).normalized.z) * 2f;
            Vector3 rightDirection = new Vector3(pilotModelView.transform.right.x, 0, pilotModelView.transform.right.z) * 10;
            Vector3 leftDirection  = new Vector3(-pilotModelView.transform.right.x, 0, -pilotModelView.transform.right.z) * 10;

            //forward cast
            bool isLandCast = Physics.Raycast(pilotModelView.transform.position + (checkpointDirection + Vector3.up).normalized * 20,
                                              Vector3.down, out hit, maxDistance, groundMask);
            isLandCast = true;
            if (isLandCast)
            {
                // находим расстояние до рейкаста до земли
                float pilotToCastPointDistance = Vector3.Distance(pilotModelView.transform.position, hit.point) * 2;

                // смотрим есть ли противник спереди или по бокам
                isEnemyOnForward = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale, hit.point,
                                                   Quaternion.identity, pilotToCastPointDistance, enemyMask);
                //isEnemyOnLeft = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale, leftDirection,
                //        Quaternion.identity, pilotToCastPointDistance, enemyMask);
                //isEnemyOnRight = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale, rightDirection,
                //        Quaternion.identity, pilotToCastPointDistance, enemyMask);

                // смотрим есть ли препятствие на пути
                isObstacleOnMyWay = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale / 2, hit.point, out hit,
                                                    Quaternion.identity, pilotToCastPointDistance, TrackEntityMask);
                if (isObstacleOnMyWay)
                {
                    if (hit.collider.tag.Equals("SlowPoint"))
                    {
                        isRightHit = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale / 2, (hit.point + rightDirection).normalized,
                                                     Quaternion.identity, pilotToCastPointDistance, TrackEntityMask);

                        isLeftHit = Physics.BoxCast(pilotModelView.transform.position, pilotModelView.transform.lossyScale / 2, (hit.point + leftDirection).normalized,
                                                    Quaternion.identity, pilotToCastPointDistance, TrackEntityMask);
                    }
                }
            }

            if (isEnemyOnForward && shipModelView.PrimaryAbility != null)
            {
                HandleActionInput(this, Vector3.forward);
            }
            //if(isEnemyOnLeft && shipModelView.PrimaryAbility != null)
            //    HandleActionInput(this, Vector3.left);
            //if(isEnemyOnRight && shipModelView.PrimaryAbility != null)
            //    HandleActionInput(this, Vector3.right);

            // создание вектора движения и поворота
            float moveH;
            if (isObstacleOnMyWay && shipModelView.autoBoostHelper == false)
            {
                if (isRightHit == false)
                {
                    moveH = Vector3.SignedAngle(shipModelView.transform.forward,
                                                shipModelView.transform.forward + rightDirection, Vector3.up);
                }

                else if (isLeftHit == false)
                {
                    moveH = Vector3.SignedAngle(shipModelView.transform.forward,
                                                shipModelView.transform.forward + leftDirection, Vector3.up);
                }
                else
                {
                    if (runOutDirection == 2)
                    {
                        runOutDirection = UnityEngine.Random.Range(0, 2);
                    }

                    if (runOutDirection == 0)
                    {
                        moveH = Vector3.SignedAngle(shipModelView.transform.forward,
                                                    shipModelView.transform.forward + leftDirection * 4, Vector3.up);
                    }
                    else
                    {
                        moveH = Vector3.SignedAngle(shipModelView.transform.forward,
                                                    shipModelView.transform.forward + rightDirection * 4, Vector3.up);
                    }
                }
            }
            else
            {
                moveH = Vector3.SignedAngle(shipModelView.transform.forward,
                                            aimOffset - shipModelView.transform.position, Vector3.up);
            }

            Vector3 direction = new Vector3(moveH / 30, 0, aimInterest);
            shipModelView.SteeringInput(direction);

            Debug.DrawLine(shipModelView.transform.position, aimOffset);
        }
    }
    private void HandleMovingInput(object sender, Vector3 direction)
    {
        shipModelView.SteeringInput(direction);

        directionArrow.ShipDirection = shipModelView.transform;
    }