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.

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

    bRAIN
    Participant

    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.
    #27488

    CodersExpo
    Participant

    Not 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.
    #27509

    bRAIN
    Participant

    Thank 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.
    #27578

    CodersExpo
    Participant

    It 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

    #27610

    bRAIN
    Participant

    Thank you it seems to work :)

    P.S : There is a feature to put a post in “resolved” (this is my first topic).

    #28217

    prime
    Keymaster

    Whenever 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.

    #28233

    CodersExpo
    Participant

    I 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.

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

You must be logged in to reply to this topic.