News Forums RAIN General Discussion and Troubleshooting How to destroy a character though RAINaction

This topic contains 6 replies, has 3 voices, and was last updated by  tapticc 2 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #11036

    HiddEnigma
    Participant

    Hello!

    I’m a newbie programmer, and I’m testing RAIN to see how far I can go by myself. I’m making a behavior tree where, when a character’s health reaches 0, it is destroyed. I can only see that working through a custom action.

    Problem is, I don’t know how to use the Destroy() function within that custom action script. How would I go about destroying a character using that custom action script?

    Thank you in advance.

    • This topic was modified 2 years, 7 months ago by  HiddEnigma.
    #11045

    HiddEnigma
    Participant

    Nevermind, I managed to make the behavior tree destroy the game object using

    MonoBehaviour.Destroy (ai.Body);

    But no matter where I put that in my custom action — Start, Execute OR Stop — the game always initializes with the gameObject already missing.

    In my behavior tree, I have a Selector with a constraint ( health > 0 ) that animates the character’s movement, and outside of the constraint, the custom action.

    So kinda like Selector - > constraint - > animates if health > 0
    - > else destroys.

    Any thoughts on what I’m doing wrong?

    • This reply was modified 2 years, 7 months ago by  HiddEnigma.
    #11065

    CodersExpo
    Participant

    Good job working it out. Make sure you add that statement in a condition within the custom action.

    if(ai.WorkingMemory.GetItem<float>(“health”) <= 0)
    {
    MonoBehaviour.Destroy(ai.Body);
    }

    Destroy, as you know, is a MonoBehavior. I personally like keeping Unity scripts and logic separate. Just a preference. Another way to do this is create a mono script attached to your guy that has a method in it to destroy him. You can use send message to execute the method.

    //From RAIN custom action script
    ai.Body.gameObject.SendMessage(“KillGuy”);

    //MonoBehavior
    public void KillGuy()
    {
    //public variable “MyGuy” has the character object assigned
    Destroy(MyGuy);
    }

    Anyway, there are many ways to kill a cat (lol I kill myself)…hope this helps

    #11069

    HiddEnigma
    Participant

    Thanks for the help! Yeah, I originally thought of using the ai.WorkingMemory, but then I thought that, if I’m using a constraint for all my actions while alive ( health > 0f), shouldn’t the customAction be called only if my character’s already dead (health < 1f)?

    Even before I thought of ai.WorkingMemory, I thought of just slapping a script onto my character, and leave it at that. It’d work, sure, but I thought that it’d be better for it to be represented in the Behaviour Tree, since it’s kinda a behaviour as well.

    • This reply was modified 2 years, 7 months ago by  HiddEnigma.
    #11099

    CodersExpo
    Participant

    Oh I see…yes you can do this…

    -Root
    -Selector
    —Constraint health >= 100 (Add the health variable in the AIRig memory. Also don’t put the f after 100 in the constraint.)
    —-Animation or Move Nodes for example should repeat or have some sequence of node that will return a success.
    —Custom Action to destroy (This node will not run if the constraint node and its child nodes sequence return a success.

    #11101

    CodersExpo
    Participant

    Sorry, constraint health > 0

    #32882

    tapticc
    Participant

    Cheers for the example, this helped me figure out how to swap out a character with one AI for another character with its own mesh and AI.

    I have a SpawnController game object (tagged as well) with this script attached:

    public class SpawnController : MonoBehaviour {
    public GameObject newZombie;

    public void SpawnZombie(Vector3 newLocation,Quaternion newRotation)
    {
    Instantiate (newZombie, newLocation, newRotation);
    }
    }

    My “old” character has this script attached:

    public class NerdController : MonoBehaviour {
    private SpawnController spawnController;

    // Use this for initialization
    void Start () {
    spawnController = GameObject.FindGameObjectWithTag (“SpawnController”).GetComponent <SpawnController> ();
    }

    public void NerdToZombie()
    {
    spawnController.SpawnZombie (gameObject.transform.position, gameObject.transform.rotation);
    Destroy(gameObject);
    }
    }

    The behaviour tree calls a Custom Action which sends a message to any script attached to the game object with a function called “NerdToZombie”:

    public override ActionResult Execute(AI ai)
    {
    ai.Body.gameObject.SendMessage(“NerdToZombie”);

    return ActionResult.SUCCESS;
    }

    I’m sharing this as it’s really difficult to piece together example snippets when you don’t know what to look for

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.