Example #1
0
    // Called once per physics frame (different than render frame)
    // FixedUpdate() is typically used instead of Update() for anything involving rigidbodies/physics
    private void FixedUpdate()
    {
        //Replace these with player input
        jumpDown = (Input.GetKey(KeyCode.Space) || Input.GetKeyDown(KeyCode.Space)) && !die;
        jumpStay = false;
        die      = (GetComponent <Collider2D>().IsTouching(enemy.GetComponent <Collider2D>()) && dead == false || transform.position.y < -4);
        Adown    = Input.GetKey(KeyCode.A);
        Ddown    = Input.GetKey(KeyCode.D);
        if (Adown && Ddown)
        {
            x = 0;
        }
        else if (Adown && !dead)
        {
            x = -1;
        }
        else if (Ddown && !dead)
        {
            x = 1;
        }
        else
        {
            x = 0;
        }

        //Calls Move every physics frame.
        controller.Move(x, jumpDown, jumpStay);
        if (die)
        {
            dead = true;
            die  = false;
            controller.Die();
        }
    }
Example #2
0
    // Called once per physics frame (different than render frame)
    // FixedUpdate() is typically used instead of Update() for anything involving rigidbodies/physics
    private void FixedUpdate()
    {
        seperation  = Mathf.Abs(transform.position[0] - player.transform.position[0]);
        closeEnough = (seperation < 5);

        if (GetComponent <Collider2D>().IsTouching(player.GetComponent <Collider2D>()))
        {
            x    = 0;
            dead = true;
        }

        if (transform.position.x < -9.75 || transform.position.x > 9.75)
        {
            x     = -x;
            timer = Random.Range(1f, 3f);
        }

        else if (closeEnough && dead == false)
        {
            if ((transform.position[0] - player.transform.position[0]) < 0)
            {
                x = 1;
            }
            else if ((transform.position[0] - player.transform.position[0]) > 0)
            {
                x = -1;
            }
        }

        else if (closeEnough == false)
        {
            //Taken from https://answers.unity.com/questions/770857/whats-the-most-efficient-way-to-time-events.html
            timer -= Time.deltaTime;
            if (timer < 0)
            {
                x     = -x;
                timer = Random.Range(1.5f, 4f);
            }
        }
        ;

        //Calls Move every physics frame.
        controller.Move(x, jumpDown, jumpStay);
        if (die)
        {
            controller.Die();
        }
    }
Example #3
0
    private void FixedUpdate()
    {
        //Replace these with AI input. It should wander until the player gets near, then chase the player.
        x        = 0;
        jumpDown = false;
        jumpStay = false;
        die      = false;

        //Calls Move every physics frame.
        controller.Move(x, jumpDown, jumpStay);

        if (die)
        {
            controller.Die();
        }
    }
Example #4
0
    private void FixedUpdate()
    {
        //Replace these with player input
        x        = 0;
        jumpDown = false;
        jumpStay = false;
        die      = false;

        //Calls Move every physics frame.
        controller.Move(x, jumpDown, jumpStay);

        if (die)
        {
            controller.Die();
        }
    }