Switch between 2 Characters with one input
I have looked for hours in the unity forms and on YouTube but i can’t seem to figure out how to switch between 2 character with their own scripts i made prefabs of the two models with attached scripts in c# first i tried doing it with the script that spawns the player by instantiate the prefab and destroying the first one but doesn’t do anything i need a script and someone to explain this I am a noob im not sure what im missing but all help is appreciated //cant figure out how to post my code
You need to explain better what you mean by "switch between 2 character with their own scripts"
Do yo humean switch which the camera is looking at? Do yo mean switch which is getting input?
Did you ever figure out how to do this? I'm in the same boat I need to switch between two characters ingame using the same input.
I'm not an expert but, you could make an empty game object, attach a "GameController" script which will be always instantiated. From that script you can monitor the input and Instantiate/Destroy the objects you wish :)
Please consider using some kind of punctuation in the future. This post is close to unreadable.
Answer by drakedane · Jun 23, 2017 at 06:00 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
Destroy prefabs after they have been instantiated (C#) 1 Answer
I need help with SyncVar. 0 Answers
destroying instantiated groups of objects 1 Answer
Unable to delete instance of GameObject 0 Answers