Adding damage to a equipped weapon with a click event

sneekyo

Member
What I want to do is be able to; from a on click even on a UI button add +5 (for example) to damage of an item within our inventory. Or 5% increased fire rate, which I imagine would be more difficult due to the animations?
 
You can adjust any usable item's use rate with UsableItem.UseRate. UseRate is seconds between uses, so you must decrease UseRate to increase use speed (i.e. to increase by 5%, you would divide by 1.05). ShootableWeapon, MeleeWeapon, ThrowableItem and Explosion all have the DamageAmount property that you can directly edit during runtime.
 
Hi Andrew, holy moly, changing the damage was easy. Thank you!

Going back to weapon speed. Can you give me a little example or get me started, I can see that I would need to create a subclass of UsableItem,
then would I create a private void of the UsableItem.UseRate? How do I select the correct weapon to adjust the UseRate? Sorry again for my lack of knowledge but I feel like I learn a little more ever time you teach me something.

Thank you for your time and patience Andrew, you are the man.

-Seth
 
-Update, I found Use.Rate in the melee/shootWeapon. And that I can adjust that on OnClick events to adjust weapon speed. My idea is to be able to upgrade weapons, speed and damage. If I adjust these with the built in runtime OnClick I can only set the amount. So if the player wanted to upgrade their pistol twice I would need it to add +5 damage rather than just set the amount, or like you mentioned above divide for speed. So I imagine I'll need to create my own scripts to adjust these rather than use what's built in. As I mentioned above could you help me get started on this? I really appreciate it.
 
Once you've got a reference to the ShootableWeapon, for example, you can just set or modify the UseRate or DamageAmount directly:
C#:
shootableWeapon.UseRate /= 1.05f;
// or
shootableWeapon.UseRate = shootableWeapon.UseRate / 1.05f;
In terms of getting the reference, if it's a simple enough use case then you could just have shootableWeapon be a public variable that you can set in the inspector. But I assume you'll need to be able to reference it during run-time, in which case you could iterate through the character's current inventory with Inventory.GetAllItems, select one (e.g. based on its name) and then get its ShootableWeapon component (or any other ItemAction component) from its ItemActions array. The "Item Amounts" example script on this page shows how to get the ItemActions from an Item reference.
 
Hi Andrew, man I'm trying but I feel like I'm stumbling in the dark.
So far this is all I have and I am pretty sure even what I have is completely wrong:


using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items.Actions;


public class WeaponSpeedIncrease : MonoBehaviour
{

[SerializeField] protected GameObject m_Character;


public void Start()
{
var inventory = m_Character.GetComponent<InventoryBase>();
if (inventory == null)
{
return;
}



var item = Inventory.GetAllItems;
if (item != null)
{
return;
}

}
}
 
Here's a basic idea:
C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Items.Actions;

public class ItemTest : MonoBehaviour
{
    void Start() {
        Inventory inventory = GetComponent<Inventory>(); // Get a reference to the character's Inventory
        
        foreach (Item item in inventory.GetAllItems()) { // Loop through all Items in the character's inventory (including items not currently held)
            ItemType itemType = (ItemType)item.ItemDefinition; // Get the ItemType of the current Item
            if (itemType.ToString() == "AssaultRifle") { // Use the ItemType's name to check if it's the one we want to modify
                foreach (ItemAction itemAction in item.ItemActions) { // Loop through all ItemActions of the Item
                    if (itemAction is ShootableWeapon) { // Specify the ShootableWeapon ItemAction
                        ShootableWeapon shootableWeapon = (ShootableWeapon)itemAction; // Get the ShootableWeapon component from the ItemAction
                        shootableWeapon.DamageAmount += 5f; // Increase damage
                        shootableWeapon.UseRate /= 1.05f; // Increase use rate
                    }
                }
            }
        }
    }
}
I've commented each line so hopefully it makes sense, but it basically follows the loop I outlined in my last post: iterate through all items to find the one you want to modify, iterate through the ItemActions of that item to find the ShootableWeapon (or MeleeWeapon, etc) component, then modify the UseRate, DamageAmount, etc properties of the ShootableWeapon.
 
Since you said you're doing this from a UI, you'd want to take something like the code in Start from my example and add it to a Button's onClick listener. You can do button.onClick.AddListener(myMethod) to make your button run the method specified when it's clicked.
 
Top