Forum rules - please read before posting.

Make an NPC do that with this

Hi

I want to add a companion to my main character. I would like that companion to be able to be given instructions such as "companion use this on that". Is that possible with AC?

The inventory will be with the player, and the companion will be next to them. The companion won't be moveable, they'll follow the player blindly using the navmesh. So I'm thinking of something like being able to click on the companion character, then on the inventory object, then on the Hotspot item to make the companion use that item on that hotspot.

Would that work with AC? If not, is there a way to get that work, or even a terminology I can search the manual for?

Olly

Comments

  • I'd say the best way to handle this would be to convert the companion into a Player character, enable Player-switching in the Settings Manager, and assign the companion as an additional Player.

    Then, when their Hotspot is clicked, you actually switch to this character with the Player: Switch Action.

    The advantage with this technique is that you can then use the Player: Check in your Interaction ActionLists to determine which Player is currently being controlled - so that you can give the two characters different responses.

    With this technique, though, AC will need to be in control of the companion's presence in the scene. Rather than placing the prefab into the scene manually, you'd edit their start data in the Settings Manager, and use the Player: Teleport inactive Action to teleport them to different scenes as needed.

    A tutorial on AC's Player-switching system can be found here.

  • edited March 3

    Thanks Chris.

    Should I use eventmanager.onsetplayer to catch when the SwitchPlayer action occurs? Before the new character is used by the player, I need to do some things such as replace the decorative and static gameobject on their back with the "real" gameobject and also fire off some anims.

    Some of that can be done as part of the movement animations, but some of it needs to be handled by a script before the second player moves an inch/cm.

    Can I also disable character switching from code so that I only enable it when I get to that part of the story? I'm assuming so.

    I'm also going to have overwrite the PlayerStart settings for the other player, as they follow my default player around on their back, so the playerstart is basically a Marker above the default player.

    I suppose, in essence, I need to create the second player when required, move it to the right spot, play some anims, then give AC control over it. Is that doable?

    Olly

  • Should I use eventmanager.onsetplayer to catch when the SwitchPlayer action occurs?

    Events are the best way to inject custom functionality, yes. Though, you're also able to use the Events Editor to run ActionLists if you prefer to avoid scripting.

    Can I also disable character switching from code so that I only enable it when I get to that part of the story? I'm assuming so.

    No, affecting the Player-switching option at runtime should be avoided.

    When enabled though, it's still the case that switching only occurs when you run the logic to do so. Under the hood, it essentially means that AC is keeping tabs on where each listed Player is in the game - you don't technically even need to switch Player when it's enabled.

    I suppose, in essence, I need to create the second player when required, move it to the right spot, play some anims, then give AC control over it. Is that doable?

    With the Player-switching option, AC will need to be in control over the character the whole time. Actions can still be used to affect specific Players - just check Is Player? and you can then use a drop-down to filter which Player you want to affect.

    Otherwise, it'd be a case of avoiding Player-switching altogether, and trying a different approach. Another way you could tackle this would be to create a "Companion" variant of each Inventory item - placed in a separate Category - and then use two different Inventory menus: one showing regular items, another showing "Companion" items after you click the NPC. You'd then be able to create Companion-specific Hotspot interactions by assigning their variant of an Inventory item in the Hotspot Inspector.

  • I feel that I may have to roll my own solution here.

    Few more questions.

    1) Can I change the start point for the 2nd player at run time? If I can, then the 2nd player could at least start by the default Player rather than at the start of the scene.

    2) Having a 2nd inventory for the 2nd player feels like a management headache. I can see all kinds of issues arising with items not set up correctly etc. Thinking out loud when the user clicks the inventory icon in my inventory and then clicks on a hotspot, could I capture which player is active, and then manually issue a move command to the 2nd player, and then have the hotpot trigger a 2nd-player-specific interaction? I feel like I can, and know how, but just wanted to check.

    Olly

  • Can I change the start point for the 2nd player at run time? If I can, then the 2nd player could at least start by the default Player rather than at the start of the scene.

    When player-switching is enabled, you can configure PlayerStarts to only be valid for specific Players. The Player: Teleport inactive Action can also be used to teleport inactive Players at runtime.

    Having a 2nd inventory for the 2nd player feels like a management headache.

    To be clear: my suggestion for a 2nd inventory was as an alternative to having a 2nd player. If you rely on a 2nd Player character and Player-switching, you can use the Player: Check Action in your Interactions to run different Actions accordingly. The Hotspot's Walk to command will affect the active Player automatically.

    The other alternative would be to keep Player-swiching disabled, and have the Companion be a Player character local to the scene.

    This'd require a bit of custom scripting, since the default behaviour is for local Players in the scene to override the default prefab.

    What you'd need to first do is spawn the companion Player into the scene at runtime, i.e. once the scene has opened and the default has been placed there. You can do this with an Object: Add or remove Action in your OnStart cutscene.

    Then, at the time you want to switch, you'd need to manually switch Player using the KickStarter.player variable, i.e.:

    KickStarter.player = companionPlayer;
    

    AC records the active Player with an ID variable. You'd also need to set this to something unique. AC's standard behaviour for local Players is to base this on the negative scene index, but it won't be able to if you spawn the Player in at runtime. You can do this manually, however, with:

    companionPlayer.ID = -2 - SceneChanger.CurrentSceneIndex;
    

    You won't be able to use the Player: Switch Action here, as it only works with the official player-switching setting. However, you could attach a Variables component to the Compnanion, define a Bool variable named "IsActivePlayer", and then attach a script to set it automatically - which you can then check for in Variable: Check:

    using UnityEngine;
    using AC;
    
    public class SetActivePlayerBool : MonoBehaviour
    {
    
        public Variables variables;
    
        void Update ()
        {
            variables.GetVariable ("IsActivePlayer").BooleanValue = KickStarter.player == GetComponent<Player> ();
        }
    
    }
    
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.