- Home /
Question by
PizzaMacMuffin · Dec 07, 2013 at 12:34 PM ·
character2d-platformerside-scrolling
Problem with script for changing character?
im currently in a gamecraft, and i m making a game where i need to switch from character to character in a 2d sidescrolling game and ive a few errors that need sorting out
ive only been at this for a month now so please not too much hard words
public class SwitchPlayer : MonoBehaviour { GameObject player1; GameObject player2;
var player1Input : (Move);
var player2Input : (Move);
bool player1Active = true;
bool player2Active = false;
GameObject defaultCamera;
GameObject cameraPlayer1;
GameObject cameraPlayer2;
void Start (){
player1Input = player1.GetComponent("(Move)");
player2Input = player2.GetComponent("(Move)");
}
void 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);
}
}
void 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
Your answer
![](https://koobas.hobune.stream/wayback/20220613123627im_/https://answers.unity.com/themes/thub/images/avi.jpg)