Page 1 of 1

Sequencer command: Set AICharacterControl target

Posted: Sun Sep 30, 2018 3:54 pm
by Tony Li
If you use Unity's Standard Assets Third Person Character with the AICharacterControl script, you can add the script below to your project. It adds the following sequencer command:

AISetTarget(target, [subject])

where:
  • target: GameObject that the character should walk to, or 'none' to stop.
  • subject: The character with the AICharacterControl script. Defaults to the speaker.
SequencerCommandAISetTarget.cs

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{

    // Adds sequencer command AISetTarget(target, [subject])
    //
    // - target defaults to listener.
    // - subject defaults to speaker.
    public class SequencerCommandAISetTarget : SequencerCommand
    {

        public void Start()
        {
            var noTarget = GetParameter(0) == "none";
            var target = noTarget ? null : GetSubject(0, listener);
            var subject = GetSubject(1, speaker);
            var ai = (subject != null) ? subject.GetComponent<UnityStandardAssets.Characters.ThirdPerson.AICharacterControl>() : null;
            if (target == null && !noTarget)
            {
                Debug.LogWarning("Dialogue System: Sequencer: AISetTarget(" + GetParameters() + "): Can't find target " + GetParameter(0));
            }
            else if (subject == null)
            {
                Debug.LogWarning("Dialogue System: Sequencer: AISetTarget(" + GetParameters() + "): Can't find subject " + GetParameter(0));
            }
            else if (ai == null)
            {
                Debug.LogWarning("Dialogue System: Sequencer: AISetTarget(" + GetParameters() + "): Subject " + subject + " doesn't have an AICharacterControl script", subject);
            }
            else if (noTarget)
            {
                if (DialogueDebug.LogInfo) Debug.Log("Dialogue System: Sequencer: AISetTarget(target=(none), subject=" + subject + ")");
                ai.SetTarget(null);
                ai.agent.isStopped = true;
            }
            else
            {
                if (DialogueDebug.LogInfo) Debug.Log("Dialogue System: Sequencer: AISetTarget(target=" + target + ", subject=" + subject + ")");
                ai.SetTarget(target);
            }
            Stop();
        }
    }
}