- Home /
Switching colliders without falling through the world?
I've got a game in which you switch between four different characters at will. I'm using the packaged CharacterController script, meaning it supplies gravity and collision while a character is active. While a character is inactive, though, I disable it and swap the CharacterController out with a SphereCollider and apply gravity to the Rigidbody.
The problem I'm running into is that this collider switch is not instantaneous. In switching, my characters end up just slightly inside any surface they're standing on, sometimes causing them to fall through the world. Is there some way to prevent this?
My character switching script is as follows:
//These are just for in-editor visibility.
var boyActive = player1Active;
var girlActive = player2Active;
var roboActive = player3Active;
var tinyActive = player4Active;
var player1 : GameObject;
var player2 : GameObject;
var player3 : GameObject;
var player4: GameObject;
var player1Input : BoyController;
var player2Input : GirlController;
var player3Input : RoboController;
var player4Input : TinyController;
static var player1Active : boolean = true;
static var player2Active : boolean = false;
static var player3Active : boolean = false;
static var player4Active : boolean = false;
var defaultCamera : GameObject;
var cameraPlayer1 : GameObject;
var cameraPlayer2 : GameObject;
var cameraPlayer3 : GameObject;
var cameraPlayer4 : GameObject;
var stepBack = 1;
function Start(){
player1Input = player1.GetComponent(BoyController);
player2Input = player2.GetComponent(GirlController);
player3Input = player3.GetComponent(RoboController);
player4Input = player4.GetComponent(TinyController);
}
function Update(){
//These are just here for in-editor visibility.
boyActive = player1Active;
girlActive = player2Active;
roboActive = player3Active;
tinyActive = player4Active;
if ((player1.GetComponent(CharacterController).isGrounded)&&
(player2.GetComponent(CharacterController).isGrounded)/*&&
(player3.GetComponent(CharacterController).isGrounded)*/&&
(player4.GetComponent(CharacterController).isGrounded))
{
if(Input.GetKeyDown(KeyCode.Alpha1)) {
player1Active = true;
player2Active = false;
player3Active = false;
player4Active = false;
}
if(Input.GetKeyDown(KeyCode.Alpha2)) {
player1Active = false;
player2Active = true;
player3Active = false;
player4Active = false;
}
if(Input.GetKeyDown(KeyCode.Alpha3)) {
player1Active = false;
player2Active = false;
player3Active = true;
player4Active = false;
}
if(Input.GetKeyDown(KeyCode.Alpha4)) {
player1Active = false;
player2Active = false;
player3Active = false;
player4Active = true;
}
}
if (!player1Active){
player1.GetComponent("SphereCollider").enabled = true;
player1.GetComponent("CharacterController").enabled = false;
player1.GetComponent("Rigidbody").useGravity = true;
}
if(player1Active){
player1.GetComponent("SphereCollider").enabled = false;
player1.GetComponent("CharacterController").enabled = true;
player1.GetComponent("Rigidbody").useGravity = false;
switchToPlayer(player1Input, player2Input, player3Input, player4Input, cameraPlayer1);
player1.transform.Find("WeightOfOne").tag = "Untagged";
player2.transform.Find("WeightOfOne").tag = "WeightOfOne";
player3.transform.Find("WeightOfOne").tag = "WeightOfOne";
player4.transform.Find("WeightOfOne").tag = "WeightOfOne";
}
if (!player2Active){
player2.GetComponent("SphereCollider").enabled = true;
player2.GetComponent("CharacterController").enabled = false;
player2.GetComponent("Rigidbody").useGravity = true;
}
if(player2Active){
player2.GetComponent("SphereCollider").enabled = false;
player2.GetComponent("CharacterController").enabled = true;
player2.GetComponent("Rigidbody").useGravity = false;
switchToPlayer(player2Input, player1Input, player3Input, player4Input, cameraPlayer2);
player1.transform.Find("WeightOfOne").tag = "WeightOfOne";
player2.transform.Find("WeightOfOne").tag = "Untagged";
player3.transform.Find("WeightOfOne").tag = "WeightOfOne";
player4.transform.Find("WeightOfOne").tag = "WeightOfOne";
}
if (!player3Active){
player3.GetComponent("SphereCollider").enabled = true;
player3.GetComponent("CharacterController").enabled = false;
player3.GetComponent("Rigidbody").useGravity = true;
}
if(player3Active){
player3.GetComponent("SphereCollider").enabled = false;
player3.GetComponent("CharacterController").enabled = true;
player3.GetComponent("Rigidbody").useGravity = false;
switchToPlayer(player3Input, player1Input, player2Input, player4Input, cameraPlayer3);
player1.transform.Find("WeightOfOne").tag = "WeightOfOne";
player2.transform.Find("WeightOfOne").tag = "WeightOfOne";
player3.transform.Find("WeightOfOne").tag = "Untagged";
player4.transform.Find("WeightOfOne").tag = "WeightOfOne";
}
if (!player4Active){
player4.GetComponent("SphereCollider").enabled = true;
player4.GetComponent("CharacterController").enabled = false;
player4.GetComponent("Rigidbody").useGravity = true;
}
if(player4Active){
player4.GetComponent("SphereCollider").enabled = false;
player4.GetComponent("CharacterController").enabled = true;
player4.GetComponent("Rigidbody").useGravity = false;
switchToPlayer(player4Input, player1Input, player2Input, player3Input, cameraPlayer4);
player1.transform.Find("WeightOfOne").tag = "WeightOfOne";
player2.transform.Find("WeightOfOne").tag = "WeightOfOne";
player3.transform.Find("WeightOfOne").tag = "WeightOfOne";
player4.transform.Find("WeightOfOne").tag = "Untagged";
}
if(player1Active==false){player1.animation.Play ("Disco_Idle");};
if(player2Active==false){player2.animation.CrossFade ("Disq_Idle",0.2);};
if(player3Active==false&&!RoboRaise.headIsUp){player3.animation.Play ("Rodeo_Idle");};
if(player3Active==false&&RoboRaise.headIsUp){player3.animation.CrossFade ("Rodeo_DepIdle",0.2);};
if(player4Active==false){player4.animation.CrossFade ("Mulligan_Idle",0.2);};
}
function FixedUpdate(){
player1.transform.position.z = 0;
player1.transform.rotation.z = 0;
player1.transform.rotation.x = 0;
player2.transform.position.z = 0;
player2.transform.rotation.z = 0;
player2.transform.rotation.x = 0;
player3.transform.position.z = 0;
player3.transform.rotation.z = 0;
player3.transform.rotation.x = 0;
player4.transform.position.z = 0;
player4.transform.rotation.z = 0;
player4.transform.rotation.x = 0;
}
function switchToPlayer(inputToEnable, inputToDisable1, inputToDisable2, inputToDisable3, cameraLocation){
inputToEnable.enabled = true;
inputToDisable1.enabled = false;
inputToDisable2.enabled = false;
inputToDisable3.enabled = false;
defaultCamera.transform.position = cameraLocation.transform.position;
defaultCamera.transform.rotation = cameraLocation.transform.rotation;
}
Answer by ThermalFusion · Aug 13, 2012 at 08:39 PM
Try moving your switching logic into FixedUpdate, as it should be used while dealing with physics.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Bullet fired at enemy only applying damage when the enemy moves 1 Answer
Help with my double jump script 1 Answer