|
Hi everybody, how can i tweak the FPS Character Controller to make my character can RUN and CROUCH?, Thank you.
(comments are locked)
|
|
The basic idea is simple: use different speeds when walking, running or crouching, and to crouch reduce the character vertical scale (remember to fix the character position while standing up to avoid falling through the floor). But if you are using the First Person Controller that comes with Unity, however, this may be a really hard job: it uses CharacterMotor.js, a very complicated script that usually drives crazy anyone who try to modify it. I know this very well: I was one of them... but I survived, and learned enough to do some tricks - including this! EDITED: @bacca87 posted a C# version below, and also included a fix to avoid run speed while in the air - feature that I've added to this answer. EDITED 2: The script above works, but has the bad collateral effect of modifying the vertical scale of any character child - like the weapon, as @Fishman92 noticed! To avoid this, the version below modify the character height instead of its vertical scale: NOTE: The white capsule childed to the FPC ("Graphics") isn't modified by this script, thus it will sink a little in the terrain when crouching. A similar problem will happen when the player crouches under a low ceiling: the top of the capsule will penetrate the ceiling. This makes no difference, since "Graphics" doesn't have a collider. Hi friend, really nice job, works 100% smooth, THANK YOU.
Sep 08 '11 at 03:29 PM
Edyvargas
To someone who wants to know how to do this exactly, follow this steps: (First you must have the "First Person Controller" that came with Unity, placed on your Hierarchy, you can found it on the standard assets on Character Controller).
Works very good, thanks again to the creator for sharing this code.
Sep 08 '11 at 03:43 PM
Edyvargas
please mark aldonalettos answer as the 'accepted' (little checkmark top-left of this post). This will give him his well-deserved karma points :)
Sep 08 '11 at 06:46 PM
SisterKy
@SisterKy, unfortunately we don't gain any karma when our answer is accepted anymore :(
Sep 09 '11 at 12:13 PM
aldonaletto
Your doubt isn't stupid: if Update looped continuously, the work would be wasted. But that's not what actually happens - each loop iteration follows a sequence: Unity first calls all Update functions in all scripts, then renders the frame and transfers it to the screen. That's why most code in Unity goes in Update: any changes done in Update affect the frame that's going to be rendered next.
Feb 19 at 11:38 AM
aldonaletto
(comments are locked)
|
|
This is the C# version of the script with a little fix that prevent the character to run while jumping. Good point! Accelerating the character while in the air is really a stupid collateral effect. I'll include this fix in my answer. Thanks!
Apr 24 '12 at 11:16 AM
aldonaletto
HI brilliant C# script very use full for my little project... But: Is it possible to make the vscale smaller then 0.5f..cause when i make 0.3 first person controller is falling trough floor .... regards: Atlantis
Apr 25 '12 at 07:50 AM
atlantis
Brilliant! Saved me so much time, works perfectly.
Jun 13 '12 at 06:46 AM
Richard 3
Thank you very much for the Script! However, I have a problem where the character would fall through the Terrain or penetrate into a concave wall when I stand up, which I believe is the cause of the capsule collision (character controller) scaling from 50% to 100% its original size and thus penetrating through colliders. Any help on the script to address this issue is greatly appreciated!
Aug 13 '12 at 05:38 PM
yohan
This was a great help, appreciated!
Oct 24 '12 at 08:51 PM
LeakySink
(comments are locked)
|

Thank you it helped alot. But i followed the istructions by edyvargas and it said i could not place i on the character beacouse the name did not match, so i had to change the name from RunAndCrouch.js to just RunAndCrouch.
But thanks anyway.
Hi viktor15328, sorry for the misunderstood, the ".js" its automatic, it means the file format, in this case "javascript" (the first comment is already edited), the name then, would be only "RunAndCrouch", or any other name you preffer to use, the name of the script can be whatever you want, but try to give it a name that helps you work in your project. Good Luck!.
Hi Guys, I find it works well, but my weapons are also shrinking ! Maybe if children object would not be affected somehow that would fix it. I havn't seemed to be able to get around it so far and i would like to get my FPS character to CROUCH and LEAN as well. Is anyone else getting that effect? (i started using unity in March!Need more input lol)
Works great! I just have 1 problem: when i release all buttons after sprinting the character starts sliding... any help?
My first person controller seems to sprint whenever I go sideways at the same speed as it is meant to be sprinting, even when I am not holding the sprint button. Can I have some help with this please?