News › Forums › Troubleshooting › Get the parent of a AIRig in a script
This topic contains 6 replies, has 3 voices, and was last updated by CodersExpo 2 weeks, 5 days ago.
-
AuthorPosts
-
June 11, 2022 at 6:14 pm #27458
Hello,
I’m new to UNITY and therefore RAIN.
I have a problem: I have a EmptyObject in which I have a model(3D) and AI (AIRig) in his Behavior Tree I use a script in C # (custom action).
When I edit my script I do not have the opportunity to make an instantiation (like this): (Transform bullet = Instantiate (bomb, pos, transform.rotation) as Transform;).
I can not either change the properties of the transform relative (of first EmptyObject).How can I fix this?
Best regards
-
This topic was modified 3 weeks, 5 days ago by
bRAIN.
June 12, 2022 at 5:19 am #27488Not sure what you’re after but based on the title “get parent of AIRig in script” try this…
var rig = GameObject.FindGameObjectWithTag("PlayerAIRig").GetComponent<RAIN.Core.AIRig>(); Debug.Log(GameObject.FindGameObjectWithTag("PlayerAIRig").transform.name); Debug.Log(rig.transform.parent.name);
Be sure to add a tag to the players’ RAIN AI as PlayerAIRig. This is the transform with the AIRig component.
-
This reply was modified 3 weeks, 5 days ago by
CodersExpo.
June 12, 2022 at 9:02 am #27509Thank you for the quick reply.
More details about the problem:
So I have a EmptyObject (named “Enemy”) that contains other objects, a 3D model, and a AI.
In the Behavior Tree AI, I add a custom action that results in a C # script.
In this script I would like to retrieve the position and rotation of the parent object (the one named “Enemy”) in order to instantiate another object (relative to my position).
NB: The “Enemy” parent and his son have the tag “Character”I’ll try what you tell me, and will edit this message depending on the result.
[EDIT] That did not work.
In addition I can not use the method instanciate (unity says “the name does not exist in the current context”).-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 5 days ago by
bRAIN.
-
This reply was modified 3 weeks, 4 days ago by
bRAIN.
June 12, 2022 at 9:40 pm #27578It should have.
rig.transform.parent.name will be the EmptyObject (named “Enemy”). The child AI component should have a tag with the name you are looking for. My example used “PlayerAIRig” but you can add any tag name you want to this transform and then make sure it’s the same in the code.
To instantiate your game object/prefab use…
MonoBehaviour.Instantiate(new GameObject(), ai.Body.transform.position, ai.Body.transform.rotation);Perhaps I’m missing something but this is how I’m interpreting your question.
Hope this helps
June 13, 2022 at 10:36 am #27610Thank you it seems to work
P.S : There is a feature to put a post in “resolved” (this is my first topic).
June 18, 2022 at 7:22 pm #28217Whenever you are in a custom action inside a behavior tree node, your Start and Execute calls are both passed a reference to the AI. In order to get transform data on the AI, use ai.Kinematic. If you really need access to the game object itself, then use ai.Body and ai.Body.transform.
I strongly recommend against using GameObject.FindGameObjectWithTag. Very slow.
June 18, 2022 at 11:46 pm #28233I agree Prime. My bad. I thought he was accessing this from a Unity mono behavior script. I should have known when he had to do “MonoBehaviour.Instantiate” that he was in a custom ai script.
Also bRAIN, since you’re just getting started I though I would clarify, you can access all RAIN components from regular mono behavior scripts too. As Prime points out, any “Find” method will be slow. It’s better if you can assign the AIRig to a script variable. If you have to use a “Find” method, be sure to cache it the Awake or Start methods so you can easily access quicker next time you need it.
-
This topic was modified 3 weeks, 5 days ago by
-
AuthorPosts
You must be logged in to reply to this topic.