Running AI commands on a human-controlled player

I’m trying to set things up to where a human-controlled player’s character gets spawned into the world, and then after a certain event, it gets taken over by an AI controller (without losing view of the character pawn)… And then after running some AI commands, I wish to return control back to the player (again, keeping the same view the entire time, as if it were a single entity all along).

I started off by setting the default pawn class in my game mode to None, making sure that both Auto Possess Player and Auto Possess AI in my pawn class is set to Disabled.

Then, in my Level Blueprint, here’s how I’m spawning the player into the world:

[View Fullscreen][1]

And in my pawn blueprint, I’m doing this:

[View Fullscreen][3]

Pressing the F key does in fact unpossess the current pawn from the human player’s controller, effectively triggering Event Unpossessed as expected. But I can’t seem to figure out how to get an AI controller to take over the pawn. Instead, it seems I’m forced to actually spawn a new pawn using the Spawn AI From Class node, which does work, but spawns slightly offset from the current pawn (instead of the exact location, which is what i was going for). And not just that, but I can’t seem to get the view target set to this new AI character either. The view just stays on the original pawn, which has been unpossessed.

So how can I swap between a human-controlled player and an AI-controlled player, without losing the view and/or “teleporting” to a slight offset from the original human-controlled pawn?

Hi, the solution for this is a little bit complicated, it would be SO MUCH EASIER if epic actually let us create an instance of an AI controller without having to create a pawn.

my answer is based on this https://answers.unrealengine.com/questions/374052/is-it-possible-to-use-both-ai-controller-and-playe.html.

in my game logic, on event onPostLogin (which executes even if its 1 player game) i spawn an AI just like you are trying to do, however right after its spawned, i save the reference of that AI controller (into my Playstate, since it will be preserved if i change pawns or controller) and do a possess using the player controller from the event onPostLogin.

after that, you can use the pawn as usual, but got an instance of the aicontroller to be used later. when you want to lose control of the pawn and let it run the behavior tree, you send the event to the gamemode, and the gamemode will execute the possess command, using the old ai controller reference. after that, you have to make the pawn run the behavior tree again.

thats my approach, just found it out right now so i cant give too much detail on it. hope it actually helps.

about the issue of the camera. im still not sure how to fix that, ill look into that tomorrow.

i got the solution for the View Target with Blend and i think its more of a workaround because its definitely a bug. im pretty sure that if you try to bind that View Target with Blend to a hotkey inside your player controller class, you will be able to switch to the spawned pawn. see the modifications on my screenshots.

The bug is on the delay. if i dont add a delay after the event call, the View Target with Blend wont work, if i add a delay (even if its with 0 seconds) it starts to work. if thats not a bug, i dont know what is. tried using Possess event on the character that gets possessed by the AI doesnt works either, even using the references from the initial PlayerController.

I hope this works for you as its really REALLY misleading when you dont know if you are dealing with a mistake or a bug. i hope Epic see this and consider it as a report.