News › Forums › RAIN › General Discussion and Troubleshooting › How to destroy a character though RAINaction
Tagged: behavior, RAINaction, tree
This topic contains 6 replies, has 3 voices, and was last updated by tapticc 2 years, 1 month ago.
-
AuthorPosts
-
April 1, 2022 at 6:35 am #11036
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, 5 months ago by HiddEnigma.
April 1, 2022 at 8:02 am #11045Nevermind, 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, 5 months ago by HiddEnigma.
April 1, 2022 at 2:25 pm #11065Good 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
April 1, 2022 at 5:51 pm #11069Thanks 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, 5 months ago by HiddEnigma.
April 1, 2022 at 8:49 pm #11099Oh 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.April 1, 2022 at 8:50 pm #11101Sorry, constraint health > 0
August 17, 2022 at 6:42 am #32882Cheers 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
-
AuthorPosts
You must be logged in to reply to this topic.