- Home /
Switch from one player to another.
I am trying to figure out how to change between two players and have the Camera focus on the selected player. How do I select between to players by pressing 1 and 2 and have my unselected player stay where the switch took place? I want to avoid using the mouse. This script is dependant on the CameraScrolling script.
My camera focus script looks like this.
private var cameraScrolling : CameraScrolling;
private var Selected = 0;
var targets : Transform[];
function Awake () {
cameraScrolling = GetComponent (CameraScrolling);
cameraScrolling.SetTarget (targets[0], true);
for (var i=0; i < targets.Length; i++)
targets[i].gameObject.SendMessage ("SetControllable", (i == 0), SendMessageOptions.DontRequireReceiver);
}
So that works fine, but I need the function to choose between the two Players.
Answer by demize2010 · May 23, 2011 at 02:48 PM
Expanding on @Meltdown's lines here is a script that will do what you want. Sorry about the formatting, this new wisywig for UnityAnswers sucks :(
You will need to define the two player objects, and have there controllers attached to those objects. You need to edit the script to refelect the controllers name.
You will need 3 cameras. The first camera is your main camera and will be moved to the position and rotation of the other two (means you can lerp nice and you won't get a nasty black frame every now and again). The other two cameras need to be children of the player objects so that they follow them round.
var player1 : GameObject; var player2 : GameObject; var player1Input : (your controller); var player2Input : (your controller); var player1Active : boolean = true; var player2Active : boolean = false; var defaultCamera : GameObject; var cameraPlayer1 : GameObject; var cameraPlayer2 : GameObject; function Start(){ player1Input = player1.GetComponent("(your controller)"); player2Input = player2.GetComponent("(your controller)"); } function Update(){ if(Input.GetKeyDown("1")) { player1Active = true; } if(Input.GetKeyDown("2")) { player2Active = true; } if(player1Active){ switchToPlayer(player1Input, player2Input, cameraPlayer1); } if(player2Active){ switchToPlayer(player2Input, player1Input, cameraPlayer2); } } function switchToPlayer(inputToEnable, inputToDisable, cameraLocation){ inputToEnable.enabled = true; inputToDisable.enabled = false; defaultCamera.transform.position = cameraLocation.transform.position; //Consider lerping defaultCamera.transform.roatation = cameraLocation.transform.rotation; //Consider lerping }
var player1Input : (your controller);
Do I replace (your controller) with my characters name or with the control script on my character? (there's more than one control script on the characters) Sorry for the dumb questions..
No worries. Badically whichevr script catches the user input ;)
From your error below, it sounds like you've not assigned one of the objects... or the GetComponents aren't set up correctly.
Answer by Hordak · May 24, 2011 at 09:19 AM
I deleted the last line in the script since I don't want the camera to rotate with the character (it's a side scroll platformer.) I can only select my two characters in the player1 and 2 input (I don't know if that's alright or it wants direct access to the Platformercontrollerscript?)
I get this Error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) Character Switch.switchToPlayer (System.Object inputToEnable, System.Object inputToDisable, System.Object cameraLocation) (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character Switch.js:42) Character Switch.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character Switch.js:31)
Here is the script as it is now:
var player1 : GameObject;
var player2 : GameObject;
var player1Input : PlatformerController; var player2Input : PlatformerController;
var player1Active : boolean = true; var player2Active : boolean = false;
var defaultCamera : GameObject; var cameraPlayer1 : GameObject; var cameraPlayer2 : GameObject;
function Start(){ player1Input = player1.GetComponent("(PlatformerController)"); player2Input = player2.GetComponent("(PlatformerController)"); }
function Update(){ if(Input.GetKeyDown("1")) { player1Active = true; }
if(Input.GetKeyDown("2")) { player2Active = true; }
if(player1Active){ switchToPlayer(player1Input, player2Input, cameraPlayer1);
}
if(player2Active){ switchToPlayer(player2Input, player1Input, cameraPlayer2);
}
}
function switchToPlayer(inputToEnable, inputToDisable, cameraLocation){
inputToEnable.enabled = true;
inputToDisable.enabled = false;
defaultCamera.transform.position = cameraLocation.transform.position; //Consider lerping
}
Please help :(
Answer by Meltdown · May 23, 2011 at 01:56 PM
Have you tried adding a camera as a child object on each player?
Then you can simply set the active camera to the one you want...
i.e
GameObject cameraPlayer1;
GameObject cameraPlayer2;
function Update()
{
if(Input.GetKeyDown("1"))
{
cameraPlayer1.active = true;
cameraPlayer2.active = false;
}
if(Input.GetKeyDown("2"))
{
cameraPlayer1.active = false;
cameraPlayer2.active = true;
}
}
Hmm that might be an Idea, but then I'll have to use different controls each player right?
Well that's no good because it's a sidescroller, so when I add the camera as a child and I turn my character around, the camera turns 180 degrees when and "films" nothingness.
@Hordak You could just partionally disable the script controlling the player when it is not in use and then enable it when it is in use.
Sorry, but I am VERY new to unity and to scripting, so I don't quite follow what you're saying.. :(
Hi, sorry for the newbie question but what do we attach this script to? One of the cameras, a player model, the main camera or something else?
Answer by justin8567a · Oct 28, 2013 at 01:20 AM
THE BEST SOLUTION: https://www.youtube.com/watch?v=VmNmQaYi_A8
Answer by drakedane · Jun 23, 2017 at 06:01 PM
Old discussion; but took me three days to work out "Character Switch" solution; so thought I would post my solution, in case it helps someone else. I am a beginner at coding; so it is possible my code could be more efficient; but it seems to work perfectly to switch between two characters. In my game, both players have a Main Camera and a Free Look Camera Rig. So those were some of the game objects that had to be switched. Here is the code I used...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityStandardAssets.Cameras;
public class CharacerSwitch : MonoBehaviour
{
public GameObject player1;
public GameObject player2;
public GameObject cam3;
public GameObject cam1;
public GameObject rig1;
public GameObject rig2;
void Start()
{
player2 = GameObject.Find("Player2");
player1 = GameObject.Find("Player1");
cam3 = GameObject.Find("Camera3");
cam1 = GameObject.Find("Camera1");
rig2 = GameObject.Find("FreeLookCameraRig2");
rig1 = GameObject.Find("FreeLookCameraRig");
cam3.SetActive(false);
rig2.SetActive(false);
cam1.tag = "MainCamera";
cam3.tag = "Camera 2";
}
void Update()
{
if (Input.GetKeyDown(KeyCode.O)) //"O" for "Other" or "Other Player"
{
if (player1.GetComponent<CapsuleCollider>().enabled == true) //collider arbitrary, but reflects object state
{
player1.GetComponent<Rigidbody>().isKinematic = true;
player2.GetComponent<Rigidbody>().isKinematic = false;
player1.GetComponent<CapsuleCollider>().enabled = false;
player2.GetComponent<CapsuleCollider>().enabled = true;
player1.GetComponent<ThirdPersonCharacter>().enabled = false;
player1.GetComponent<ThirdPersonUserControl>().enabled = false;
player2.GetComponent<ThirdPersonCharacter>().enabled = true;
player2.GetComponent<ThirdPersonUserControl>().enabled = true;
cam1.SetActive(false);
rig1.SetActive(false);
cam3.SetActive(true);
rig2.SetActive(true);
cam1.tag = "Camera 2";
cam3.tag = "MainCamera";
}
}
if (Input.GetKeyDown(KeyCode.P)) //"P" for "Player"
{
if (player1.GetComponent<CapsuleCollider>().enabled == false) //collider arbitrary, but reflects object state
{
player1.GetComponent<Rigidbody>().isKinematic = false;
player2.GetComponent<Rigidbody>().isKinematic = true;
player1.GetComponent<CapsuleCollider>().enabled = true;
player2.GetComponent<CapsuleCollider>().enabled = false;
player1.GetComponent<ThirdPersonCharacter>().enabled = true;
player1.GetComponent<ThirdPersonUserControl>().enabled = true;
player2.GetComponent<ThirdPersonCharacter>().enabled = false;
player2.GetComponent<ThirdPersonUserControl>().enabled = false;
cam1.SetActive(true);
rig1.SetActive(true);
cam3.tag = "Camera 2";
cam1 = GameObject.Find("Camera1");
cam1.tag = "MainCamera";
cam3.SetActive(false);
rig2.SetActive(false);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612083223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to switch between 2d characters? 0 Answers
UI Button Focus 2 Answers
How to detect if gui textbox is selected? 0 Answers
Focus on/select (TMP) input field? 2 Answers