Forum begins after the advertisement:


[Part 26] – How to add bonuses like “greed”? (SOLVED!)

Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 26] – How to add bonuses like “greed”? (SOLVED!)

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #18276
    Grim Rubbish
    Level 27
    Silver Supporter (Patron)
    Helpful?
    Up
    2
    ::

    Part 26 really helped me to create a better coin collect system, thank you so much! However, I would like to know, how passive bonuses like “greed” from “PlayerStats.cs” are added to the “PlayerCollector.cs”. How can this be archieved? Thank you again and Cheers!

    EDIT: I found a solution! I also added some sound for Gold Pickup ;-)

    “`

    using System.Collections; using System.Collections.Generic; using UnityEditor.U2D.Animation; using UnityEngine;

    [RequireComponent(typeof(CircleCollider2D))] public class PlayerCollector : MonoBehaviour { PlayerStats player; CircleCollider2D detector; public float pullSpeed;

    <code>public UICoinDisplay UI;
    float coins;
    
    [Header("Audio Source")]
    public AudioSource collectableSound;
    
    [Header("Collect Gold Audio")]
    public AudioClip coinCollect;
    public float coinCollectVolume = 0.4F;
    
    [Header("Remove Gold Audio")]
    public AudioClip coinRemove;
    public float coinRemoveVolume = 0.4F;
    
    private void Start()
    {
        player = GetComponentInParent&lt;PlayerStats&gt;();
    }
    
    
    public void SetRadius(float r)
    {
        if (!detector) detector = GetComponent&lt;CircleCollider2D&gt;();
        detector.radius = r;
    }
    
    public float GetCoins() { return coins; }
    //Updates coins Display and information
    public float AddCoins(float amount)
    {
        var greed = player.Stats.greed;
    
        if (greed &gt; 0)
        { 
        coins += amount * (int)greed; 
        collectableSound.PlayOneShot(coinCollect, coinCollectVolume);
        if (UI) UI.UpdateDisplay();
        return coins;
        }
        else {
        coins += amount;
        collectableSound.PlayOneShot(coinCollect, coinCollectVolume);
        if (UI) UI.UpdateDisplay();
        return coins;
        }
    }
    
    public float RemoveCoins(float amount)
    {
        coins -= amount;
        collectableSound.PlayOneShot(coinRemove, coinRemoveVolume);
        if (UI) UI.UpdateDisplay();
        return coins;
    }
    
    // Saves the collected coins to the save file.
    public void SaveCoinsToStash()
    {
        SaveManager.LastLoadedGameData.coins += coins;
        coins = 0;
        SaveManager.Save();
    }
    
    void OnTriggerEnter2D(Collider2D col)
    {
        //Check if the other GameObject is a Pickup.
        if (col.TryGetComponent(out Pickup p))
        {
            p.Collect(player, pullSpeed);
        }
    
        if (col.TryGetComponent(out PickupResource pr))
        {
            pr.Collect(player, pullSpeed);
        }
    }</code>

    }

    
    </p>

    and 1 other person have upvoted this post.
    #18277
    Alp Apustaja
    Level 10
    Legacy AI Assistant
    Helpful?
    Up
    1
    ::

    Based on the user’s code, the passive bonus “greed” from PlayerStats.cs is added to PlayerCollector.cs in the AddCoins() method:

    <code class="language-csharp">public float AddCoins(float amount)
    {
        var greed = player.Stats.greed;
    
        if (greed &gt; 0)
        { 
            coins += amount * (int)greed; 
            collectableSound.PlayOneShot(coinCollect, coinCollectVolume);
            if (UI) UI.UpdateDisplay();
            return coins;
        }
        else {
            coins += amount;
            collectableSound.PlayOneShot(coinCollect, coinCollectVolume);
            if (UI) UI.UpdateDisplay();
            return coins;
        }
    }</code>

    The solution multiplies the coin amount by the greed stat when greed is greater than 0, effectively implementing a passive bonus to coin collection.

      1 anonymous person
    has upvoted this post.
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: