Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 cougman30 · Jul 24, 2012 at 10:44 PM · switch characters

Switching between multiple characters in game

I am looking for some help on how to switch between multiple characters in a game. I making a soccer style game, so I only have 1 camera, and it is always fixed in the ball. How do I switch between different members of my team, and switch to the closest character, after hitting the spacebar? I thought about using an empty game object, attaching the character controller to the game object, and then parent whichever character I am controlling to the empty object, but I need each character to be able to switch in and out of AI mode (which I have not yet created). So I need them to be able to either move or not move independently of what character the user is controlling that that point.

So what would be the best way to implement a character switch for this scenario? I have looked at many of the other questions posted, but have not found a good way for me to implement into my game. All characters will have the same animations (just on different models), and all characters will behave the same (for the most part). There is no big difference between the characters that I will be using.

Appreciate your help!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by darthbator · Jul 25, 2012 at 12:26 AM

I would do something like attach a Trigger collider to the ball itself. Then you could grab all the objects within the trigger and check them all with something like Vector3.distance against the ball location in order to figure out what player is closest to the ball. I would then activate a character controller component on that character and deactivate his AI component.

Since you have a static number of players you could always just create an array with all the player gameObjects and then iterate through it and just check each player for distance when hitting the switch button in order to find the closest player.

I like the first solution more because you don't need to make un-necassary distance checks for players that are nowhere near the ball's vicinity.

Comment
Add comment · Show 1 · 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 cougman30 · Jul 25, 2012 at 12:31 AM 0
Share

What about when you don't collide with the ball? How would you go about switching to a different character when your $$anonymous$$m doesn't have the ball? Would you still activate a character controller component on the character that is closest (after doing the Vector3.distance calculations)? Or do you know of a different way that could work?

avatar image
0

Answer by justin8567a · Oct 25, 2013 at 01:23 AM

http://forum.unity3d.com/threads/207001-GTA-V-Character-Switching-System-Character-Wheel-(COMING-SOON)

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

7 People are following this question.

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

Related Questions

Switching between first-person Character Controllers 1 Answer

Switching Trough Active Players using Lists 1 Answer

player to car switching ways 0 Answers

Efficient Way to Create/Destroy and Switch Between Player GameObjects 1 Answer

Changing variable numbers depending on current player model. 1 Answer


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