News Forums RAIN General Discussion and Troubleshooting Use animator of child object

This topic contains 19 replies, has 2 voices, and was last updated by  Hoju 1 year, 1 month ago.

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #33462

    Hoju
    Participant

    I’m creating a game for a group school project using a combination of 2D and 3D. My colliders are all 3D on the parent object but are using the animator and sprite renderer of a child object. This is so that the collider is moving in the Y axis while the sprites face the Z axis. So far I’ve been able to get everything in RAIN to work correctly except the animations through mecanim. I’ve narrowed it down to the fact that RAIN is only looking for the animator on the parent object and not the children. The AI object is a child of the child with the sprite renderer and animator.

    How do I get RAIN to look for the animator on the child object?

    Let me also say that I am a complete novice at coding. I am a game artist but for this group class need to do all the code. I am new but trying to learn so if coding is required I will do my best to follow along.

    #33469

    prime
    Keymaster

    Just to be clear on this - when you say “animator” you mean a Mecanim Animator? Not a legacy Animation component?

    #33470

    Hoju
    Participant

    Correct.

    #33480

    prime
    Keymaster

    I’ll throw together a script and post it here. It will be a Custom AI Element that you can add to your AI to resolve the issue. You should be able to use it to create a new script file in your project. The Custom tab of the AI will update automatically, and then you can select the new element to add it. That easy. Look for it late today.

    #33482

    Hoju
    Participant

    That would be awesome! Thank you Prime.

    #33490

    prime
    Keymaster

    Here’s the script. Create a file called LocateAnimatorElement.cs and use this as the body. Once you save the file and Unity does a recompile, the “Locate Animator” custom element should be available in the custom elements tab of your AI. Add it to your AI from the dropdown, then make sure you drag the animator you want to use into the MecanimAnimator field. On Start, the element will swap out the Mecanim Animators on the MecanimMotor and MecanimAnimator.

    using UnityEngine;
    using RAIN.Core;
    using RAIN.Serialization;
    using RAIN.Motion;
    using RAIN.Animation;
    [RAINSerializableClass, RAINElement("Locate Animator")]
    public class LocateAnimatorElement : CustomAIElement 
    {
        [RAINSerializableField(Visibility = FieldVisibility.Show, ToolTip = "Mecanim Animator to use")]
        private UnityEngine.Animator _mecanimAnimator = null;
        public override void Start()
        {
            base.Start();
            //Replace animators on the Motor and the RAINAnimator
            if (_mecanimAnimator != null)
            {
                MecanimMotor tMotor = AI.Motor as MecanimMotor;
                if (tMotor != null)
                    tMotor.UnityAnimator = _mecanimAnimator;
                MecanimAnimator tAnimator = AI.Animator as MecanimAnimator;
                if (tAnimator != null)
                    tAnimator.UnityAnimator = _mecanimAnimator;
            }
        }
    }
    #33493

    Hoju
    Participant

    Unfortunately, this script doesn’t seem to work for me. I added the script and get the custom element; then I add the the game object with the animator on it. Even saved the scene and restarted Unity for good measure, but at runtime, I still get the errors, “MecanimAnimator: No Animator present on AI Body (adding a temporary one)” and “Animater has not been initialized.” It then adds the temporary animator to the parent object. Not sure of what to even try at this point.

    #33496

    Hoju
    Participant

    Just a few other details, I do have MecanimMotor set on the motor as well as MecanimAnimator set on animator. Does RAIN look specifically for the Avatar of the animator? I am not using an Avatar as the animator is only animating the Sprite Renderer. Not sure if that has anything to do with it but thought I would mention it.

    #33499

    prime
    Keymaster

    Those should be warnings, not errors, and shouldn’t effect the outcome. RAIN will add a temporary animator, but it should be irrelevant. The custom element swaps out that animator with the one you specify. You’re sure that’s not happening?

    #33502

    Hoju
    Participant

    Yes, they were warnings not errors. At runtime, I select the parent object and look at the temporary Animator and it shows “None” under Controller.

    It only ever plays the default state. If it is connected to the animator and just not showing it, then the variables it’s calling from the AIRig and Behavior Tree are never going through to the animator for some reason. I’ve checked and rechecked my variables to make sure they are correct but so far I can’t find a mistake anywhere.

    The default state is “Idle” and when bool “Walking” is set to true, it should transition to “Walk” state (as it is set in mecanim). On the AIRig, I created an empty state. Set Mecanim State to “Walk”, Layer 0 (only layer in my animator), set Start Parameter-Parameter Name to “Walking”, Parameter Type to Boolean and Parameter Value to “true”. In Behavior Tree, created a Mecanim-Set Parameter in the part of the tree the it needs to go (this does return Success), and set the value to “Walking = true”.

    Am I missing something here? Should that not transition to the Walk state at this point in runtime?

    #33503

    prime
    Keymaster

    Ugh. I don’t like how this is put together in RAIN - we’ll fix it.

    Try using an Animate node instead of a Mecanim Set Parameter node. Let me know if that works. If not then I can give you a custom behavior tree node

    #33505

    Hoju
    Participant

    Using an Animate node instead, it will only seem to play the default state (Idle) and never switch. It also seems to be causing some erratic behavior in my Behavior Tree. I have a random node playing 3 timers of varying times and it just constantly switches between them instead of choosing one at random unless I set repeat on the Animate node to forever.

    Thanks again for your help. I know this isn’t exactly the way RAIN was designed to be used.

    #33510

    prime
    Keymaster

    I’ll post another script on Monday that will help you out. It will be a custom action that takes the place of the built-in Mecanim Parameter node. We’ll also be making a change to the default behavior of our built-in node for 2.1.5 so it makes this issue a little easier to solve.

    Thx

    #33520

    Hoju
    Participant

    Sweet.

    • This reply was modified 1 year, 1 month ago by  Hoju.
    #33522

    Hoju
    Participant

    One other, unrelated issue I’m having. This may be a stupid, simple thing but how do I rotate the AI 180 degrees in the X axis inside a custom action.

    if (ai.Motor.IsFacing(Vector3.left))
    {
    ai.Motor.FaceAt(Vector3.right);
    }
    if (!ai.Motor.IsFacing(Vector3.left))
    {
    ai.Motor.FaceAt(Vector3.left);
    }

    I have tried using this but it only works during the false if statement for some reason (the AI will look left if facing right but not vice-versa).

    Surely there is an easier way to do this. Been experimenting and playing around with it for 2 days now and can’t figure it out.

    • This reply was modified 1 year, 1 month ago by  Hoju.
Viewing 15 posts - 1 through 15 (of 20 total)

You must be logged in to reply to this topic.