Пример #1
0
    // This function check for keyboard input and update triggers for player animation

    protected override void ComputeVelocity()
    {
        Vector2 move = Vector2.zero;

        if (camanimator.enabled == true || cam.enabled == true) //Stops player from moving left or right when cinemachine brain and animator are disabled.
        {
            move.x = Input.GetAxis("Horizontal");               // Check if the player has pressed "A" or "D" on the keyboard and makes the player move left or right

            if (Input.GetKey(KeyCode.Space) && grounded)        // Check if the spacebar has been pressed
            {
                // This if statment prevents the sound effect to play repeatedly if the user spams the space key
                if (Time.time > NextActionTime)
                {
                    NextActionTime = Time.time + period;
                    Knight_SoundManager.PlaySound("Knight_Jump2"); // Play sound effect when player jump
                }
                velocity.y = jumpTakeOffSpeed;
                createDust();
            }
            else if (Input.GetKeyUp(KeyCode.Space))
            {
                if (velocity.y > 0)                 // Check if player is moving upwards
                {
                    velocity.y = velocity.y * 0.5f; // Reduce player velocity by half when the player stop pressing the spacebar
                }
            }

            // Check which direction the player is moving and flip the sprite renderer accordingly

            if (move.x > 0.01f)
            {
                if (spriteRenderer.flipX == true)
                {
                    spriteRenderer.flipX = false;
                    if (grounded == true)
                    {
                        createDust(); // Activates the particle system
                    }
                }
            }
            else if (move.x < -0.01f)
            {
                if (spriteRenderer.flipX == false)
                {
                    spriteRenderer.flipX = true;
                    if (grounded == true)
                    {
                        createDust(); // Activates the particle system
                    }
                }
            }

            animator.SetBool("grounded", grounded);                           // Triggers grounded parameter in animation if player is grounded
            animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed); // Triggers velocityx parameter in animation if player is moving

            targetVelocity = move * maxSpeed;                                 // Set the speed of the player
        }
    }
Пример #2
0
 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.gameObject.tag == "Player")
     {
         Instantiate(coinParticle, col.transform.position, Quaternion.identity); // Activates the particle system
         Knight_SoundManager.PlaySound("Power_Coin");                            // Plays the sound effect
         Power_ScoreTextScript.coinAmount += 1;                                  // Increments the counter by 1
         Destroy(gameObject);
     }
 }
Пример #3
0
    //This function will take any damage that the player receives through its parameter
    public void PlayerTakeDamage(int damage)
    {
        currenthealth -= damage;       //Subtracting the damage taken to the current health of the player

        animator.SetTrigger("hurt");
        Knight_SoundManager.PlaySound("Knight_Hurt"); // Activates sound for Hurt animation

        if (currenthealth <= 0)
        {
            Die();
        }
    }
Пример #4
0
    public GameObject heartParticle; // Enables the programmer to add a particle system in the inspector

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Player")
        {
            Instantiate(heartParticle, col.transform.position, Quaternion.identity); // Activates the particle system
            Knight_SoundManager.PlaySound("Power_Heart");                            // Plays the sound effect

            // This if statement checks if the players health is below 4 before incrementing it
            if (Knight_Combat.currenthealth < 4)
            {
                Knight_Combat.currenthealth += 1;
            }

            Destroy(gameObject);
        }
    }
Пример #5
0
    public void OpenChest(float xAmount, float yAmount)
    {
        Vector2 m_force = new Vector2(xAmount, yAmount) * thrust;   // This determines the amount of force to apply to the rigidbody

        // This for loop activates the sprite renderer of all objects in the loop
        for (int i = 0; i < loots.Length; i++)
        {
            loots[i].SetActive(true);
        }

        // This for loop applies a force to all objects in the loop
        for (int i = 0; i < loots.Length; i++)
        {
            rb2D[i].AddForce(m_force);
        }

        animator.SetTrigger("Open");                      //  Activates trigger for chest open animation
        Knight_SoundManager.PlaySound("Power_ChestOpen"); // Activates sound for opening chest
        Bcol2D.enabled = false;                           // Disables the chest's box collider to prevent the player from hitting it again
    }
Пример #6
0
    public GameObject[] loots;  // Array to contain objects

    public void BreakBarrel(float xAmount, float yAmount)
    {
        Vector2 m_force = new Vector2(xAmount, yAmount) * thrust;   // This determines the amount of force to apply to the rigidbody

        // This for loop activates the sprite renderer of all objects in the loop
        for (int i = 0; i < loots.Length; i++)
        {
            loots[i].SetActive(true);
        }

        // This for loop applies a force to all objects in the loop
        for (int i = 0; i < loots.Length; i++)
        {
            rb2D[i].AddForce(m_force);
        }

        animator.SetTrigger("break");                   //  Activates trigger for Barrel destruction animation
        Knight_SoundManager.PlaySound("Knight_Barrel"); // Activates sound for Barrel destruction
        Destroy(gameObject, .5f);                       // Destroys the game object from the scene
    }
Пример #7
0
    private void Update()
    {
        if (Time.time >= nextAttackTime) // Keeps track of the current time
        {
            if (Input.GetButtonDown("Fire1"))
            {
                Knight_SoundManager.PlaySound("Knight_Sword1"); // Plays sword sound effect
                Attack();                                       // Reference to Attack function below
                nextAttackTime = Time.time + 1f / attackRate;   // Controls the attack rate of the player per second
            }
        }

        if (Input.GetButtonUp("Fire1"))
        {
            camanimator.enabled = true; // Enables the Main Camera's Cinemachine Brain
            cam.enabled         = true; // Enables the Main Camera's animator
        }

        // Keeps the heart UI of the player up to date with the player's currenthealth as parameter
        heartSystem(currenthealth);
    }