How to tell what weapon impacted an object?

SeeSharpist

New member
Hey guys, I've been working on a harvesting system working with UCC. I have everything working atm, but I'm having trouble with one thing.

I'd like to add a component to certain weapons that mark them as Tools able to actually harvest a node, but I'm having trouble with the attackingObject param in OnImpact. It's a standard object rather than a UnityEngine.Object and is giving me Invalid cast exceptions when trying to cast it as a GameObject so that I can look for the specific component.

Is there a better way to get access to the weapon that made contact in the Melee Weapon Impact Callback? Thanks!
 

Attachments

  • ImpactCallback.png
    ImpactCallback.png
    17.5 KB · Views: 4
Casting in C# is a bit confusing and I'm not sure I fully understand it myself, but using the as operator works in this case, i.e. GameObject myGameObject = attackerObject as GameObject. I think it's because GameObject inherits from Object, and you can't do an explicit type conversion on an object into a type that's lower down in its inheritance hierarchy. If that makes any sense.
 
Hey Andrew, that makes total sense, thanks. My biggest problem was I didn't know what that object was meant to be cast as to be able to work with it. Turns out it was the Opsive MeleeWeapon obj, so this ended up working. Thanks!

C#:
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        Debug.Log(name + " impacted by " + attacker + " on collider " + hitCollider + " with " + attackerObject + ".");
        if(!this.harvested){
            Opsive.UltimateCharacterController.Items.Actions.MeleeWeapon attackerMelee = attackerObject as Opsive.UltimateCharacterController.Items.Actions.MeleeWeapon;
            GameObject impactObject = attackerMelee.gameObject;
            Debug.Log(impactObject.GetType());
            //Then check if this type of attacker has a Harvester component
            HarvestingTool toolStats = impactObject.GetComponent<HarvestingTool>();
 
Top