Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hordak · May 23, 2011 at 12:34 PM · switchkeyfocusselect

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
1

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

  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
     
             }
    
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hordak · May 23, 2011 at 03:29 PM 0
Share

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

avatar image demize2010 · May 24, 2011 at 12:35 AM 0
Share

No worries. Badically whichevr script catches the user input ;)

avatar image demize2010 · May 25, 2011 at 12:13 PM 0
Share

From your error below, it sounds like you've not assigned one of the objects... or the GetComponents aren't set up correctly.

avatar image
0

Answer by Hordak · May 24, 2011 at 09:19 AM

@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 :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
   }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hordak · May 23, 2011 at 02:21 PM 0
Share

Hmm that might be an Idea, but then I'll have to use different controls each player right?

avatar image Hordak · May 23, 2011 at 02:28 PM 0
Share

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.

avatar image FLASHDENMARK · May 23, 2011 at 02:38 PM 0
Share

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

avatar image Hordak · May 23, 2011 at 02:41 PM 0
Share

Sorry, but I am VERY new to unity and to scripting, so I don't quite follow what you're saying.. :(

avatar image CraftyMaelyss · Sep 07, 2016 at 03:09 PM 0
Share

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?

avatar image
0

Answer by justin8567a · Oct 28, 2013 at 01:20 AM

THE BEST SOLUTION: https://www.youtube.com/watch?v=VmNmQaYi_A8

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
                 }
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I Select/focus the SceneView in Editor? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges