Example #1
0
    void TrackPlayer()
    {
        targetX = MiscTools.IncrementTowards(transform.position.x, player.position.x + xOffset, acceleration);
        targetY = MiscTools.IncrementTowards(transform.position.y, player.position.y + yOffset, acceleration);

        CheckConstraints();

        transform.position = new Vector3(targetX, targetY, -10);
    }
    private void HorizontalMovement()
    {
        //Horizontal Input
        h = Input.GetAxisRaw("Horizontal");

        //If there is horizontal input, change the velocity
        if (h != 0)
        {
            //If Run input is detected, run
            if (Input.GetAxisRaw("Run") == 1)
            {
                //animator.SetFloat("Speed", Mathf.Abs(h)+1);
                //Set the velocity. Multiply by h to set direction
                rigidbody2D.velocity = new Vector2(MiscTools.IncrementTowards(rigidbody2D.velocity.x, h * maxRunSpeed, runForce), rigidbody2D.velocity.y);
            }
            //If no run input is detected, walk
            else
            {
                //animator.SetFloat("Speed", Mathf.Abs(h));
                //Set the velocity. Multiply by h to set direction
                rigidbody2D.velocity = new Vector2(MiscTools.IncrementTowards(rigidbody2D.velocity.x, h * maxWalkSpeed, walkForce), rigidbody2D.velocity.y);
            }
            //Emit particles
            if (grounded)
            {
                dustParticles.enableEmission = true;
            }
            else
            {
                dustParticles.enableEmission = false;
            }
        }
        else
        {
            dustParticles.enableEmission = false;
        }


        // If the input is moving the player right and the player is facing left...
        if (h > 0 && !facingRight)
        {
            Flip();
        }
        // Otherwise if the input is moving the player left and the player is facing right...
        else if (h < 0 && facingRight)
        {
            Flip();
        }


        //If the player is grounded and there is no more horizontal input, decelerate the player
        if (!Input.GetButton("Horizontal") && grounded)
        {
            rigidbody2D.velocity = new Vector2(MiscTools.IncrementTowards(rigidbody2D.velocity.x, 0, deceleration), rigidbody2D.velocity.y);
            //animator.SetFloat("Speed", 0);
        }

        //If the player's y position gets lower than a certain point, respawn and subtract 1 hp
        if (transform.position.y < -30)
        {
            transform.position = currentCheckpoint;
            SubtractHP(1);
        }
    }