DestroyIt and Opsive UCC

noname2u2

New member
THIS IS AN UPDATED SCRIPT FROM MODELSHARK STUDIOS DestroyIt TO WORK WITH UCC. This is from the forums on Unity by ModelShark Studio.


Hi all,

We recently had a couple questions about integrating DestroyIt with Opsive's Ultimate Character Controller. We had previously done a video tutorial for integrating DestroyIt with UFPS, but due to changes to UFPS Version 2 and Ultimate Character Controller, that video is now outdated.

Anyway, long story short - the steps to integrate DestroyIt with the newer version of Opsive's controller are different, but actually easier now. UFPS version 2 exposes an OnObjectImpact event that your game objects can listen to and apply damage to the Destructible component as needed.

I went through the process for making a crate destructible in Ultimate Character Controller/UFPS, and below are the steps, starting at the beginning.

Steps:
1) Create a new project
2) Import Ultimate Character Controller and DestroyIt asset packages
3) Open the Ultimate Character Controller UFPSDemo scene
4) Copy the DamageOnImpact script (see below) into your project
5) Choose an object in the demo scene (such as one of the crates) you want to be destructible
6) Add the Destructible script to the crate, set its total hit points to 10
7) Add the DamageOnImpact script to the crate
8) From the top menu, choose Window -> DestroyIt -> Setup - Minimal
9) Run the demo scene and shoot the destructible object with the rifle

DamageOnImpact Script:

Code (CSharp):

Code:
using DestroyIt;
using UnityEngine;
using Opsive.Shared.Events;

public class DamageOnImpact : MonoBehaviour
{
    public void Awake()
    {
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnObjectImpact);
    }

    //private void OnImpact(float amount, Vector3 position, Vector3 direction, GameObject attacker, Collider hitCollider)
    private void OnObjectImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        Destructible destructible = hitCollider.GetComponentInParent<Destructible>();
        if (destructible != null)
            destructible.ApplyDamage(amount);

        Debug.Log(name + " hit by " + attacker.name + " for " + amount + " damage.");
    }

    public void OnDestroy()
    {
        EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnObjectImpact);
    }
}


Hope that helps anyone looking to use DestroyIt with Opsive's Ultimate Character Controller (or other controllers - my understanding is they all use UFPS version 2).
 
Last edited by a moderator:
With Version 2.2 has broken the integration script. I believe this updated script should fix the integration with Version 2.2.

Code:
using DestroyIt;
using UnityEngine;
using Opsive.Shared.Events;

public class DamageOnImpact : MonoBehaviour
{
    public void Awake()
    {
        //EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
        EventHandler.RegisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnObjectImpact);
    }

    //private void OnImpact(float amount, Vector3 position, Vector3 direction, GameObject attacker, Collider hitCollider)
    private void OnObjectImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        Destructible destructible = hitCollider.GetComponentInParent<Destructible>();
        if (destructible != null)
            destructible.ApplyDamage(amount);

        Debug.Log(name + " hit by " + attacker.name + " for " + amount + " damage.");
    }

    public void OnDestroy()
    {
        //EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, Collider>(gameObject, "OnObjectImpact", OnImpact);
        EventHandler.UnregisterEvent<float, Vector3, Vector3, GameObject, object, Collider>(gameObject, "OnObjectImpact", OnObjectImpact);
    }
}
 
Top