user2888425
user2888425

Reputation: 25

Switch turns between the Player and AI in Unity

I've recently started using Unity for a resource management stealth game. The stealth part is turn based, similar to Hitman Go. I have a simple character controller and a simple patrolling AI over a specific path. However, these movements work in real time and I want to change that to turn based. The AI should wait for the player to finish his/her move and then move itself. The same goes for the player.

Both the player and AI should be able to move to their adjacent waypoints only when the movement of the other part is complete.

How should I go about that?

Thank you

The language that I'm writing in is UnityScript.

Upvotes: 1

Views: 1847

Answers (2)

Raimund Krämer
Raimund Krämer

Reputation: 1309

Write the ai as a player instance and have it emulate player input. (Instead you could also implement a common interface on both classes.)

Spawn a game object with a GameManager behaviour script that stores a reference to the current player (or ai). Then have the GameManager update the current player every frame by checking their input. If the (human) player gives input while it is not his turn, his input will just be ignored.

This way, the player and ai do not have to know if it is their turn.

Upvotes: 0

ali.turan
ali.turan

Reputation: 553

As a very simple solution, firstly you can create an empty gameobject. Name it as TurnController. With a simple script you can add a boolean variable on it. Lets name it as isPlayerTurn. For player movement you can check this, if it is true player can move. At the end of his/her move (maybe clicking end turn button or when it reachs the max distance to move or something else) you can set isPlayerTurn false. Ofcourse AI should check (Maybe in Update function. But can change by your design) if it is true, AI can do what it needs to do. And at the finish of its turn, it should change isPlayerTurn back to true. I know it is a very simple solution but hope it helps for begining. And I hope I didnt misunderstand your question.

Upvotes: 2

Related Questions