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*.gameObject.SendMessage ("SetControllable", (i == 0), SendMessageOptions.DontRequireReceiver);*

}
So that works fine, but I need the function to choose between the two Players.

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 :frowning:

  1. 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.

  2. 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
    
        }
    

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;
  }
}

@demize2010

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 :frowning:

THE BEST SOLUTION: - YouTube

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);
                }
            }
        }
    }