예제 #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        bool shoot = true; //automatic

        if (shoot)
        {
            EnemyWeaponScript weapon = GetComponent <EnemyWeaponScript>();
            if (weapon != null)
            {
                weapon.Attack(false);
            }
        }

        movePlayerVector = 1;

        //Flipping the sprite
        if (enemyRigidBody2D.position.x <= -13 && !facingRight)
        {
            Flip();
            // enemyRigidBody2D.position = new Vector2(-12,0);
            movePlayerVector = 1;
        }
        else if (enemyRigidBody2D.position.x >= 13 && facingRight)
        {
            Flip();
            movePlayerVector = -1;
        }
        enemyRigidBody2D.velocity = new Vector2(movePlayerVector * speed, enemyRigidBody2D.velocity.y);
    }
    private void UpdateEngaged()
    {
        Vector3 playerToThis = PlayerInfo.Instance.playerPos.position - transform.position;


        if (SpotPlayer())
        {
            timeSinceLastSpottedPLayer = 0f;
            spotTimer += Time.deltaTime;
            enemyTarget.transform.position = PlayerInfo.Instance.playerPos.position;
            if (playerToThis.magnitude < distanceToPlayerToStartShooting)
            {
                enemyTarget.transform.position = transform.position;
                float angleBetween = Vector3.Angle(playerToThis, transform.up);
                if (Mathf.Abs(angleBetween) > 1f)
                {
                    float      angle = Mathf.Atan2(playerToThis.y, playerToThis.x) * Mathf.Rad2Deg - 90f;
                    Quaternion q     = Quaternion.AngleAxis(angle, Vector3.forward);
                    transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * SweepSpeedInDegreesPerSecond);
                }
                if (Mathf.Abs(angleBetween) < visionAngle && weapon.CanShoot && spotTimer > spotWaitTime)
                {
                    weapon.Attack();
                }
            }
        }
        else
        {
            timeSinceLastSpottedPLayer += Time.deltaTime;
            if (timeSinceLastSpottedPLayer > playerSpottingEscapeTime)
            {
                spotTimer = 0;
                state     = AIState.Wait;
            }
        }
    }