Forum rules - please read before posting.

Moving Character in Script

edited December 2018 in Technical Q&A

Hi!

This one is a bit more elaborate. I want to write a custom Move to Point action, as there's a bunch of extra stuff I wanted to happen while moving the character. There's a couple of things I'm currently struggling with.

1) Reference character from other script. At the begging of ActionList I wanted to define a character through custom Action. Basically, you drag a character into Action and then reference this character in later Actions.

As a simple example:
ScriptA: public GameObject character;
I then added necessary contantID values:
override public void AssignValues () and assigned the ID in ShowGUI(); just like in the tutorial. https://adventurecreator.org/tutorials/getting-custom-action-work-assets

I then have another script:
ScriptB: public GameObject charRef; public ScriptA ref;

and in Run()
charRef = ref.character;

From what I understand, this should do the trick, but Unity gives me an error "Object reference not set to an instance of an object" when I try to run ScriptB.

I'm wondering if that's possible within AC at all, or if I'm making a silly mistake somewhere. I also considered using constantID to access the character from ScriptA but I can't seem to find a way to do so. Seems like my Inspector isn't even displaying ConstantID in the inspector.

2) Move non-player character. Once I access the character, I wanted him to move to a specific marker.

I went over the tutorial on Writing an Interaction Through Script (https://adventurecreator.org/tutorials/writing-interaction-through-script), but in the tutorial, everything is referenced through the Inspector, so I can't figure out specific parts that I need.

I can use KickStarter.player.MoveToPoint(); to move the player, but what if I wanted to move another character (ideally from point 1 above).

I also have a marker in the scene, that I want to use as the spot to move to. Again, the tutorial uses the Marker assigned through Inspector, but is there a way to access the marker in the scene directly through code? The Scripting Guide seems to have just a brief description about markers.

3) Speech - Lastly, I want the character to say a line from script. So far, I've managed to only get the player character to say a line through:KickStarter.dialog.StartDialog(KickStarter.player, "Test line."); but how do I get a different character to say this line?

I wasn't sure if I should have split all these questions into separate Discussions on the forum, but I didn't want to spam the board.

Comments

  • edited December 2018

    Don't worry about spamming the board - separating different issues up makes it easier for future users with similar problems to find them. These three problems are generally related, however.

    1. I'm assuming ScriptA / ScriptB are Actions, in which case you can't expose "ScriptA" as a field in your Action's GUI - since it's not something you can specifically select in the Hierarchy / Project windows. You're not saying which line in ScriptB is actually producing the error, but I'm assuming it's when you try to set the value of "ref".

    While you could make a variable reference to the ActionList that contains it, and then try to extract the Action within that, it's more convenient to simply rely on a GameObject parameter in place of your character.

    In the ActionList's Inspector, define a new "GameObject" parameter named e.g. "My Character". You can then use that to override e.g. the "Character to move" field in the Character: Move to point Action, the "Speaker" field in the Dialogue: Play speech Action, and so on. To set the value of this parameter at the start of the ActionList, have your first Action be an ActionList: Set parameter Action, through which you can assign "My Character" as the character you wish to move.

    1. This and 3. both involve referring to components through script, which is a general Unity operation and not one specific to AC - which is what the Scripting Guide focuses on.

    To assign an NPC component attached to a GameObject named "MyNPC", you can use GameObject.Find:

    NPC myNPC = GameObject.Find ("MyNPC").GetComponent <NPC>();
    

    You can also use GameObject.FindWithTag to get a GameObject tagged with something unique, or just make the variable public so that it's exposed in the Inspector:

    public NPC myNPC;
    

    For more examples, see Unity's own Scripting Reference.

  • Thank you so much!!!

    Parameters is exactly what I was looking for. I've seen them in tutorials before, but I didn't quite grasp the concept behind them or how I can make use of them.

    I've set a parameter for the character, so I don't have to keep dragging the same character into each Play Speech Action. Instead I can select the parameter. I also have managed to set my custom actions to use the parameter, so that's all I was looking for really.

    There's still a problem with getting the character to talk in script though. In a custom Action I want to replace:
    KickStarter.dialog.StartDialog(KickStarter.player, "Test line.");
    with
    KickStarter.dialog.StartDialog(character, "Test line.");
    Character in this case being the parameter I've set at the beginning of the ActionList. Based on the Writing Interaction in Script tutorial, this should work, but instead I get an error: "Cannot convert from 'UnityEngine.GameObject' to 'AC.Char'. The parameter set is actually a dragged in character gameObject.

    Also, is there a way to refer to a Marker in the scene through script? Or would I have to use a parameter for that?

  • edited December 2018

    The parameter set is actually a dragged in character gameObject.

    The parameter type "GameObject" just needs a GetComponent call to convert it into the Char component, i.e.:

    GameObject myParameterValue = parameters[0].gameObject;
    Char myCharacter = myParameterValue.GetComponent <Char>();
    

    is there a way to refer to a Marker in the scene through script? Or would I have to use a parameter for that?

    You can use either - the "GameObject" parameter is a catch-all for any Unity component, since you can use GetComponent on the referenced GameObject to get the right script.

    The Marker component itself is rarely used by Action Run() functions directly - what's actually important is the Transform component instead, i.e.:

    Marker myMarker;
    Transform markerTransform = myMarker.transform;
    Vector3 markerPosition = markerTransform.position;
    
  • This is awesome, I've managed to get everything working. Thank you for the reply! :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.