- Home /
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!
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.
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?
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
Follow this Question
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