Search Unity

scale character controller but not player?

Discussion in 'Scripting' started by phil.cooper@mtcl.net, May 5, 2010.

  1. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    i am using the following script to scale the character controller for when the player crouches.

    it scales the controller perfectly but also scales the geometry of the player.

    does anyone know how i can fix this?

    Code (csharp):
    1.  
    2. var controller : CharacterController;
    3.  
    4.  
    5. function Update ()
    6. {
    7.    if (Input.GetKey   ("c")) {
    8.       GetComponent(CharacterController);transform.localScale.y = 0.5;
    9.    } else {
    10.       GetComponent(CharacterController);transform.localScale.y = 1;
    11.    }
    12.    }
     
  2. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    I guess what you want to do is change the collider? Then just do something like this:
    Code (csharp):
    1. characterController = GetComponent(CharacterController);
    2.  
    3. if (Input.GetKey   ("c"))
    4. {
    5.     characterController.height = 0.5;
    6.     characterController.center = 0.25;
    7. }
    8. else
    9. {
    10.     characterController.height = 1.0;
    11.     characterController.center = 0.5;
    12. }
    However, scaling the controller like that might cause unwanted behavior in some cases.
     
  3. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    that has stopped the player scaling which is great but now he has sunk into the floor to knee level.

    any ideas?
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You have semicolons in the middle of your lines of code where you're trying to use dot syntax:
    Regardless, "transform" is not a property of a character controller. What you probably want to use is the height property.

    Also, your code will run faster if you don't run GetComponent all the time. What I do is just define a variable and drag the Game Object, from the Hierarchy, onto the variable slot:

    Code (csharp):
    1. var controller : CharacterController
    2. ...
    3. controller.height = .5
    Edit: Someone else responded while I was replying. In order to not have the character go through the floor, you can move the center of the controller down by .25 when he crouches.
     
  5. DominoOne

    DominoOne

    Joined:
    Apr 22, 2010
    Posts:
    433
    Well, that's a bit strange. Have you changed the center of the controller, as I wrote in the code sample?
     
  6. phil.cooper@mtcl.net

    phil.cooper@mtcl.net

    Joined:
    Nov 10, 2009
    Posts:
    265
    ok ive sorted the sinking issue.

    at the moment i have to take my finger off the 'walk' button and then press the 'crouch' button. but what i want to be able to so is press the 'crouch' button whilst pressing the 'walk' button.

    is this possible?