- Home /
How do I change what player I want to controll when there is multiple players?
Hi! I am thinking of making a game where one got multiple players in first person veiw and controll, but I would like to know how to make a script that changes the player one would be playing on!
I've tried remaking my target enemy script to a change player script but I cant make it work.
Game will be single player of course.
Thanks fot your help!
Answer by Peter G · Mar 05, 2011 at 08:14 PM
So you want to switch between multiple players in your level.
You have several options of various difficulty with various advantages. The simplest would be to give each character a bool isControllable
then make that true or false based on the situation.
function Update () {
if(isControllable) {
//Normal input cycle.
}
}
Then you would have a script that changes who is controllable.
function ChangePlayableCharacter (newCharacter : PlayerControllerScript) {
oldCharacter.isControllable = false;
newCharacter.isControllable = true;
}
The advantage of this is that it is very easy to set up. In fact it is so easy that all's you have to do is add one conditional in your player controller script. The disadvantage, you have a lot of wasted cpu cycles calling Update() on all your players when only one of them will respond to events at any given time.
A more complicated, but alternative method. Have all your player scripts implement an interface a few simple commands.
public interface CommonPlayerMethods {
void tick();
//we will use this instead of Update() to manually call our player.
void DisablePlayer();
void EnablePlayer();
}
This is really just for good programming's sake. I don't know how many different player controller scripts you have, but this way, you can guarantee that these methods are legal on any class that implements this interface. Then have a script that calls the methods:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ActivePlayerManager : MonoBehaviour { public event Action firePlayerTick; private CommonPlayerMethods currentPlayer;
public void Start () {
StartCoroutine( TickManager() );
}
public IEnumerator TickManager () {
for(;;) {
if(firePlayerTick != null)
firePlayerTick();
yield return null;
}
}
public void ActivatePlayer (CommonPlayerMethods newPlayer) {
if(currentPlayer != null) {
firePlayerTick -= currentPlayer.tick;
currentPlayer.DisablePlayer();
}
firePlayerTick += newPlayer.tick;
//Could someone confirm this as correct syntax for adding a method to a generic event.
//I was surprised I didn't have to explicitly put a delegate wrapper around it.
currentPlayer.EnablePlayer();
currentPlayer = newPlayer;
}
}
Ok, let's go over what this does, or is at least supposed to do, I haven't tested it. It has two variables, an event, and an instance of CommonPlayerMethods. The event isn't really needed for this example, but for someone looking at this later with a question about turning multiple AI characters on and off might find it useful. Thanks to polymorphism, any class that implements that interface is acceptable. Because we don't care what class it is, as long as it can call those 3 methods. In Start()
we tell it to start an infinity loop that we will use to manage the player loop.
In TickManager()
we call trigger the event which will call any delegates attached to it. Then we wait a frame and do it again. What's this sound like, Update()
, correct, we just created a custom Update() function. So if you put your normal Update()
code in tick()
, it will get called every frame just like if it were in Update()
.
In ActivatePlayer()
we switch between players, first we remove the old delegate then we add the new one. With only one delegate attached to the event at any given time it is a bit frivolous, but this way it would be easier to modify to accompany multiple controllable players.
Even if you don't use an event model and just call the methods directly, the advantage to manually calling player methods is that you will have much fewer wasted CPU cycles from un-needed Updates. The disadvantage is that it is harder to set up.
Answer by Nathan Bennett · Mar 05, 2011 at 08:34 PM
You could just implement a counter. a player with the highest counter is controllable, but more so like the person above me- this will take alot of CPU usage if not done correctly. using a counter, you can add alot of different situations in a game; like most times swapped or maybe have a limit on how many times you can swap player. your the coder...Code. i find it alot more better learning curve personally if i don't give you the code. this way people have to experiment with it- and find the way that works for them... coding is like 3D Modeling, your completely free to implement your imagination and like modeling your only restricted through the performance of your system and your experience. i hope that helps :D
Your answer
Follow this Question
Related Questions
script help gameobject to playercar 1 Answer
Player moves faster when fps is higher? 1 Answer
Change AI Follow Players 1 Answer
Player object carried from scene to scene 1 Answer
How do I get each GameObject to mind its own business? 1 Answer