Exemple #1
0
    protected override void Update()
    {
        if (Time.time > nextDistanceCheckTime)
        {
            nextDistanceCheckTime = Time.time + distanceCheckingInterval;

            myWidth    = entity.width;
            enemyWidth = enemySensing.nearestEnemy.width;

            Vector3 nearestEnemyPosition = enemySensing.nearestEnemy.transform.position;
            Vector3 myPosition           = entity.transform.position;

            float   widthFactor     = myWidth + enemyWidth; //multiply the resulting distanceVectorBythisFactor to also use width
            Vector3 distanceVector  = nearestEnemyPosition - myPosition;
            float   distanceToEnemy = (distanceVector - distanceVector.normalized * widthFactor).sqrMagnitude;

            movement.MoveTo(nearestEnemyPosition + (myPosition - nearestEnemyPosition).normalized * (perfectShootingDistance + myWidth + enemyWidth));

            if ((nearestEnemyPosition - myPosition).sqrMagnitude > maxShootingDistance)
            {
                inRange = false;
                movement.StopLookAt();
                weapon.StopAiming();
                //Debug.Log("stop aim");
            }
            else
            {
                inRange = true;
                // Debug.Log("start aim");

                movement.LookAt(enemySensing.nearestEnemy.transform);
                weapon.AimAt(enemySensing.nearestEnemy);
            }
        }

        if (inRange)
        {
            if (weapon.CanShoot())
            {
                weapon.Shoot();
            }
        }
    }
Exemple #2
0
    public override void UpdateComponent()
    {
        switch (state)
        {
        case MissileFighterState.Idle:

            if (sensing.nearestEnemy != null)
            {
                state = MissileFighterState.AttackingEnemy;
            }

            break;

        case MissileFighterState.AttackingEnemy:

            break;

        case MissileFighterState.AttackingBase:

            break;
        }

        switch (shootingState)
        {
        case ShootingState.TooNear:

            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;

                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;
                if (distanceToEnemy > minimalShootingDistance)
                {
                    shootingState = ShootingState.InShootingDistance;
                    movement.Stop();
                }
                else
                {
                    //move to a position perfectly in the shooting range
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                    weapon.AimAt(sensing.nearestEnemy);
                    if (weapon.CanShoot())
                    {
                        weapon.Shoot();
                    }
                }
            }

            break;

        case ShootingState.TooFarAway:

            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;

                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;
                if (distanceToEnemy < maximalShootingDistance)
                {
                    shootingState = ShootingState.InShootingDistance;
                    movement.LookAt(sensing.nearestEnemy.transform);
                    weapon.AimAt(sensing.nearestEnemy);
                    movement.Stop();
                }
                else
                {
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
            }
            break;

        case ShootingState.InShootingDistance:

            //1. check if we need to change state
            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;
                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;

                if (distanceToEnemy < minimalShootingDistance)
                {
                    shootingState = ShootingState.TooNear;
                    movement.LookAt(sensing.nearestEnemy.transform);
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
                else if (distanceToEnemy > maximalShootingDistance)
                {
                    shootingState = ShootingState.TooFarAway;
                    movement.StopLookAt();
                    weapon.StopAiming();
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
                else
                {
                    if (weapon.CanShoot())
                    {
                        weapon.Shoot();
                    }
                }
            }

            break;
        }
    }