Search Unity

fps crouch script help

Discussion in 'Scripting' started by felixfors, Oct 5, 2010.

  1. felixfors

    felixfors

    Joined:
    Aug 13, 2010
    Posts:
    178
    Hey Im creating a fps game and need some help with my crouch script.
    I want to crouch when i holding down the C button and stop crouching Im stop holding the C button down but thats not the problem, the problem is thats when Im starting to crouch everything works fine but when I will stop crouching and stop holding down the C button I fall through the floor and keep falling down. if you got time to check out the script it would be nice : )

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

    and one more question, is it possible to do so I will move slowler when Im crouching? " in the same script"


    Thanks for reading and hope you can help me out : )
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    I had a problem like this along time ago. i cant remember how i fixed but i have an idea that may work. I think that the reason your falling through the floor is cause the CC scales back faster than the collision so it goes past it. If you lift your CC up 0.5 just before scaling it when C is lifted up then it will have some room to scale back out.:idea:

    Oh if i were you i would just add this code to the walker/motor script then you can control the speed var at the same time on one script with simple operators.:)
     
  3. Ocelottango

    Ocelottango

    Joined:
    Aug 4, 2010
    Posts:
    8
    It seems to me that when you hit c the character controller is pushed through the object, and you fall. Similar to if you were to position the object inside another with a collider, and start the game. I would try shrinking the objects high rather than moving it down.
    http://unity3d.com/support/documentation/ScriptReference/Transform.html
    The Local scale might work for this, but im not sure, monkey with it a bit.
    As for Slowing down when your holding the key, I would just make another function that set the speed for the character controller (speed is a variable) lower while the key was held, and normal while it's not.
    What black mantis said
     
    Last edited: Oct 5, 2010
  4. felixfors

    felixfors

    Joined:
    Aug 13, 2010
    Posts:
    178
    I still falling through the floor : ( but thansk for reply : )
     
  5. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    If your adjusting your position before scaling as posted above, lets see your new code to see if your doing it right. Then a few more ideas could be thrown your way.
     
  6. Irishsun530

    Irishsun530

    Joined:
    Jun 23, 2012
    Posts:
    13
    Did you ever get this worked out? I'm having the same problem. First person shooter that uses the character motor and fpscontroller.
     
  7. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I was having the same problem a month or so back. The way I solved it was adjusting the center point of the character controller down a bit when he crouches. And then bring it back up again when they stand. I don't remember how that worked exactly but it did. If your still having problems I can go into more detail.
     
  8. wolfstien786

    wolfstien786

    Joined:
    Apr 12, 2012
    Posts:
    185
    Just like roger0 said.... You'll have to decrease the center.y of your character controller when you crouch and increase it when you uncrouch. This is because when you crouch by decreasing the height of the character controller, it decreases from both top as well as bottom. Because of this you'll get a very bad crouching effect. When you try to uncrouch by increasing the height the same thing happens. But this time you'll probably fall off the floor / terrain because your height increases faster than the floor collision detection. So in order to stop this, if you increase your center.y before increasing your height, the character controller will have enough room to expand, thus preventing you from falling off. You will have to adjust the center.y value until it works for you just fine.

    As a sidenote... Use Mathf.SmoothDamp to change your height and center.y values to get a nice smoothed out crouching effect. :)
     
  9. BDX777

    BDX777

    Joined:
    Dec 21, 2013
    Posts:
    14
    You just established a variable for your character controller, then didn't even use it.

    Why are you using GetComponent when you already have established "var controller"?
    Also, why is there a semicolon in between "GetComponent()" and "transform.localScale.y"?
    If you want to adjust the height of your character via CharacterController, try controller.height, otherwise you absolutely don't need "var controller" at all and what you really need to do is adjust transform.localScale.y ONLY. And this toggle doesn't really work either.

    I'm also stuck on trying to figure out how to not get my character to not fall through the floor. I'm using C#.