- Home /
Player or character select
How do I make it so I can change players for the user to control. For instance so '1' selects tank, '2' selects helicopter? Similar to the character/player select system used in beat-em-ups/shmups.
Answer by StephanK · Jul 31, 2010 at 07:46 PM
I'd try having a manager object with references to all players/vehicles control scripts. Then if you press a certain button I'd activate one and deactivate all other vehicle's control script.
Answer by spiel2001 · Jul 31, 2010 at 09:30 PM
Try something like this: Create an empty game object called "PlayerInput" and attach the movement control script to it including the following...
// note... each character controller must have a camera defined // that represents the viewpoint for that controller
var defaultController : int = 0; var movementSpeed : double = 5.0;
var controllers : CharacterController[]; var cameras: Camera[];
private var currentCharacter; private var currentController;
function Start() { currentCharacter = defaultController; currentController = controllers[currentCharacter];
for( i = 0; i < cameras.length; i++ )
{
if( i == currentCharacter ) cameras[i].enabled = true;
else cameras[i].enabled = false;
}
}
function Update() { // player changing views...
if( Input.GetKeyDown( 'v' ) )
{
cameras[currentCharacter].enabled = false;
currentCharacter++;
if( currentCharacter == controllers.length )
currentCharacter = 0;
currentController = controllers[currentCharacter];
cameras[currentCharacter].enabled = true;
}
// movement for whichever character the player is currently controlling
// assumes Y is the up axis
var moveDirection = Vector3( Input.GetAxis ( "Horizontal" ), 0, Input.GetAxis ( "Vertical" ) );
currentController.Move( transform.TransformDirection( moveDirection ) * movementSpeed * Time.deltaTime );
}
// EDIT: corrected a bug in the code example
Your answer
Follow this Question
Related Questions
Understanding Prefabs + Player Spawn Points 2 Answers
Input Script And Mecanim 0 Answers
Alan Wake style 3rd person character controls, help! 1 Answer
My character jumps infinitely, even when in mid-air 1 Answer
2D Movement Problems 2 Answers