- Home /
Inactive player continues moving if switching player while moving
Basically I have two players, Cube1 and Cube2, with their own character controllers and cameras. I can switch between them by pressing keys 1 and 2. While moving Cube1, Cube 2 is immobile and vice versa. My problem is that if I'm moving a character and switch to the other character while I'm still holding down either the WASD keys or arrow keys I use to move, it will keep on moving as if a move button is still being held down (not because of physics. having the other player continue to be effected by physics when I switch is what I want to happen).
Here is the code I'm using. Both cubes have the default Unity FPS scripts: Character Controller, Character Motor, and FPSInput Controller..
var player1 : GameObject;
var player2 : GameObject;
var player1Input : FPSInputController;
var player2Input : FPSInputController;
var cameraPlayer1 : GameObject;
var cameraPlayer2 : GameObject;
function Start(){
player2Input.enabled = false;
cameraPlayer2.active = false;
}
function Update(){
if(Input.GetKeyDown("1")) {
//player1Active = true;
cameraPlayer1.active = true;
player1Input.enabled = true;
cameraPlayer2.active = false;
player2Input.enabled = false;
}
if(Input.GetKeyDown("2")) {
//player2Active = true;
cameraPlayer1.active = false;
player1Input.enabled = false;
cameraPlayer2.active = true;
player2Input.enabled = true;
}
}
As long as switching doesn't stop a character from being affected by physics it's find. I just need to stop the inactive character from wandering off. Also any suggestions as to how I can handle this script better if I want to add a lot more characters is most welcome :) . Scripting is not my strong point.
Your answer
