• 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
1
Question by Jamalakins · Jun 07, 2014 at 11:53 PM · 2d-platformermain camera2d controller

How to switch between player controllers

I have two 2D Character Controllers in my game from the unity Sample Assets beta package and I want to be able to switch control from one to the other and back with the press of a button and have the camera focus on the new character. The method I came up with was to attach a script to each character meaning when I press a button it disables the controller scripts on one character while enabling them on the other. Here's the code thus far:

 var script; 
  
 function Update() {
   
   if(Input.GetKeyDown(KeyCode.P))
   script = GetComponent("Platformer2DUserControl"); 
   script.enabled = false;
   }
   {
   
   if(Input.GetKeyDown(KeyCode.P))
   script = GetComponent("PlatformerCharacter2D"); 
   script.enabled = false;
   }

I then have the same script attached to my other controller only replacing 'false' with 'true.' This kinda works but very poorly and with some animation problems. Also I don't know how to switch back to the previous character controller but pressing the same button. I have no idea how to do the camera. Any help would be really appreciated, thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer Wiki

Answer by Dizzyman572 · Jun 08, 2014 at 01:02 AM

Ah, I see you are going for The Cave gameplay style by Double Fine. Very nice. :) Okay, this may require some changes to your project a bit. Instead of attaching this to your characters, make an empty GameObject and call it a GameManager. Having a GameManager is really great and will be far more efficient to use than if the characters handled the switching themselves. Plus, it's really flexible! If you use Arrays with your GameManager you could possibly have as many characters as your heart desires. The code I've written for you obviously isn't capable of that, but it's should be good base to get you going. :)

Here's the one for the GameManager.

 var Character1 : Transform = null;
 var Character2 : Transform = null;
 
 
 function Update () {
 
     if(Input.GetKey(KeyCode.N)){
 
             Character1.gameObject.SendMessage("Activate");
             Character2.gameObject.SendMessage("Deactivate");
         }
 
     if(Input.GetKey(KeyCode.B)){
             
             Character2.gameObject.SendMessage("Activate");
             Character1.gameObject.SendMessage("Deactivate");
         }
 
 }


Now here's the code for the Characters. (Pay attention to how I treat the boolean, very important)

 var inputEnabled : boolean = false;
 
 
 // Update is called once per frame
     function Update () {
 
         if(inputEnabled == true){
             transform.Translate(Vector3.right * 5 * Input.GetAxisRaw("Horizontal") * Time.deltaTime);
             transform.Translate(Vector3.up * 5 * Input.GetAxisRaw("Vertical") * Time.deltaTime);
         }
     
     }
 
     function Activate(){
         inputEnabled =true;
     }
 
     function Deactivate(){
         inputEnabled =false;
     }
Comment
Add comment · Show 8 · 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 Jamalakins · Jun 08, 2014 at 09:17 AM 0
Share

Thanks for the reply! I set up the scripts as you said but it's still not working. I'm not sure why because when I press "n" and "b" the "Input Enabled" variable toggles off and on so I know it registers but both my controllers remain active. Do you know what I did wrong?

avatar image Jamalakins · Jun 08, 2014 at 09:28 AM 0
Share

I've just noticed the "disabled character" slows down and the jump is less powerful but it's still active. $$anonymous$$aybe this can help you understand the problem?

avatar image Dizzyman572 · Jun 08, 2014 at 04:56 PM 0
Share

Hmm, this is very odd indeed. Try using Debug.log to help find your problem. Also, if you can post more code in your comments, that would be helpful. http://docs.unity3d.com/ScriptReference/Debug.Log.html

avatar image Jamalakins · Jun 08, 2014 at 05:45 PM 0
Share

I just copied the code exactly as you put it in. I edited the code into this:

 var inputEnabled : boolean = false;
  
  
 // Update is called once per frame
     function Update () {
  
        if(inputEnabled == true){
          script = GetComponent("Platformer2DUserControl"); 
           script.enabled = true;
   }
   {
   script = GetComponent("PlatformerCharacter2D"); 
   script.enabled = true;
   }
             }
  
     function Activate(){
        inputEnabled =true;
     }
  
     function Deactivate(){
        inputEnabled =false;
     }

And this kind of worked if I started with the controllers deactivated (although I could only start with one or both not one then the other) so I know your code is working but it's not effecting the controllers in the right way. I understand all of your cod except this bit:

 if(inputEnabled == true){
          transform.Translate(Vector3.right * 5 * Input.GetAxisRaw("Horizontal") * Time.deltaTime);
          transform.Translate(Vector3.up * 5 * Input.GetAxisRaw("Vertical") * Time.deltaTime);
        }

Do you think you could explain it? $$anonymous$$aybe if I understand it I can figure out what went wrong.

avatar image Dizzyman572 · Jun 10, 2014 at 07:56 AM 1
Share

Way to go Jamalkins. I'm glad I can be of help. Don't be afraid to come back to Unity Answers if you have any more questions. :)

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 People are following this question.

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

Related Questions

A Quick Question About 2D Enemy AI 0 Answers

2D Mobile controller help [C#] 0 Answers

How to apply different animations on different platforms in a 2d game? 0 Answers

How to switch between 2d characters? 0 Answers

Rotate around, but track the mouse 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges