Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by reberk · Aug 13, 2012 at 07:04 AM · javascriptcolliders

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; 
 
         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image reberk · Aug 13, 2012 at 08:41 PM 0
Share

Ah! You're absolutely right, of course. Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges