x


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.

more ▼

asked Apr 26 '12 at 08:07 AM

Confused gravatar image

Confused
41 8 10 13

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x291
x237
x60
x30

asked: Apr 26 '12 at 08:07 AM

Seen: 350 times

Last Updated: Apr 26 '12 at 08:07 AM