How to know when Movement/Rotation is done

Hello friends. I need help executing an action after a former action is completed.

In my current script, when I set the bools onTheRotate or onTheMove to true, the gameobject will rotate to or move to the target and when it is finished, will set that bool to false. I’m not sure if this is the best way, but it works great.

Here is a sample of some code:

    void Update()
    {
        if (onTheRotate)
            RotateTowardsTarget(Targets[whatTarget]);
        if (onTheMove)
            MoveTowardsTarget(Targets[whatTarget]);
    }

//Function that moves gameObject towards target
    private void MoveTowardsTarget(Vector3 v)
    {
        transform.position = Vector3.MoveTowards(transform.position, v, movementSpeed * Time.deltaTime);

        if (Vector3.Distance(transform.position, v) < 0.0001f)
        {
            transform.position = v;
            onTheMove = false;
        }
    }

//Function that moves gameObject towards target
    private void RotateTowardsTarget(Vector3 v)
    {
        Quaternion targetRotation = Quaternion.LookRotation(v - transform.position);

        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotateStrength * Time.deltaTime);

        if (Quaternion.Angle(transform.rotation, targetRotation) < 0.0001f)
        {
            transform.rotation = targetRotation;
            onTheRotate = false;
        }
    }

What I need now is to know when this is done and trigger the next “action.”

The AI might have an order of:
1: Rotate toward target
2: Move toward target
3: Change target
4: Rotate towards target
5: Interact with target
6: etc.

I really have no idea how to do this, other than to, for example, set onTheMove to true at the end of my rotate function. This works fantastic, but only in cases where movement follows rotation, which is not all the time.

Thank you for your help.

Ed

You need a finite state machine. Finite-state machine - Wikipedia

Don’t be intimidated by the scary sounding name. All that means is that your AI should keep track of a current “State” of what it’s currently doing. The current state determines what the current behavior of your AI is. Each state also has certain conditions under which it transitions to another state. For example:

Your AI can start in the RotateTowardsTarget state. In that state, the behavior is to rotate towards the target. Once it is facing the target, it will transition to the MoveTowardsTarget state. When in that state, the behavior is to move towards the target. It might have two ways to transition out of that state. For example if the target stops being in front of your AI, it will transition back to the RotateTowardsTarget state. Alternatively, if it reaches the target, it will then transition into the InteractWithTarget state, and so on.

You can easily turn a MonoBehaviour into an FSM with three pieces:

  • an enum to define the different states (and a variable on your MonoBehaviour of that enum type).
  • a switch statement inside Update() to do different behavior depending on the current state.
  • A method that switches the current state to another state, to be called within a state when necessary to transition to the next state.
1 Like

Like @PraetorBlue suggests, a FSM is a very simple(relative to AI, but yes, it’s also very beginner friendly if you’re unfamiliar with AI but can code)

But when you say you want the AI to take certain actions… it can make a FSM pretty complex (in sheer size, not logic complexity), if the behaviors are highly dynamic i suggest looking into Goal Oriented Action Planning

if you never did AI before start with learning about FSM and see if your AI can be simplified to one.

Interesting. The State Machine seems like my solution.

This AI is going to be extremely basic in size and scope. It will simply follow commands in a specific order (programmed by the player) and will not respond to external stimuli (such as detect player in range). These AI will be programmed by the player to do simple actions, such as move there → harvest resource → come back → deposit