// Update is called once per frame
    void FixedUpdate()
    {
        // Distance of enemy from player
        float distance = Vector3.Distance(this.transform.position, playerTransform.transform.position);

        if (distance < minChallengeDistance)
        {
            challenged = true;
        }
        else if (distance > maxChallengeDistance)
        {
            challenged = false;
        }

        // Actions when enemy is challenged
        if (challenged)
        {
            directionOfPlayer = (playerTransform.position - this.transform.position).normalized;        // Get direction to move towards

            this.transform.Translate(directionOfPlayer * speed * Time.deltaTime, Space.World);          // Move enemey to player
            this.transform.LookAt(playerTransform, worldUp);                                            // Points torwards Player


            // Firing rate
            timer -= Time.deltaTime;
            // Always shooting
            if (true)
            {
                if (timer <= 0)
                {
                    Vector3 thisDirection = this.transform.rotation * Vector3.forward;

                    // Initial position for the projectile
                    Vector3 intialPosition = this.transform.position + thisDirection;

                    // Instantiate bullet with intialPosition and random rotation
                    GameObject bulletObject = (GameObject)Instantiate(bullet, intialPosition, Random.rotation);

                    // Get the script
                    ProjectileMover projectileMover = (ProjectileMover)bulletObject.transform.GetComponent("ProjectileMover");

                    // Set origin of the projectile
                    projectileMover.setOrigin(this.gameObject);

                    projectileMover.fire();

                    // Reset
                    timer = firingRate;
                }
            }
        }
        else if (!challenged)
        {
            // Random idle animations
        }
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        float currSpeed = speed * Time.deltaTime;               // Current Speed

        float xInput = Input.GetAxis("Horizontal");             // Inputs
        float yInput = Input.GetAxis("Vertical");

        Vector3 mousePosition = Input.mousePosition;

        playerMover.velocity = new Vector2(xInput * currSpeed, yInput * currSpeed);             // Velocity

        // Mouse position
        Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, 10));

        this.transform.LookAt(pos, worldUp);

        // When either xInput or yInput isn't 0, recalculate rotation
        if (xInput != 0 || yInput != 0)
        {
            playerMover.rotation = Mathf.Atan2(xInput, yInput) * -180 / Mathf.PI;                       // Rotation
        }

        //Press space to activate/deactivate upgrades
        if (Input.GetButton("Jump"))
        {
            PeaShooterBehavior.upgradeDamage = !PeaShooterBehavior.upgradeDamage;
            PeaShooterBehavior.upgradeSpeed  = !PeaShooterBehavior.upgradeSpeed;
        }

        // Firing rate
        timer -= Time.deltaTime;
        if (Input.GetButton("Fire1"))
        {
            if (timer <= 0)
            {
                Vector3 thisDirection = this.transform.rotation * Vector3.forward;

                // Initial position for the projectile
                Vector3 intialPosition = this.transform.position + thisDirection;

                // Instantiate bullet with intialPosition and random rotation
                GameObject bulletObject = (GameObject)Instantiate(bullet, intialPosition, Random.rotation);

                // Get the script
                ProjectileMover projectileMover = (ProjectileMover)bulletObject.transform.GetComponent("ProjectileMover");

                // Set origin of the projectile
                projectileMover.setOrigin(this.gameObject);

                projectileMover.fire();

                // Reset
                timer = firingRate;
            }
        }
    }