Well, as the Topic says I need my FPS Controller crouch. But I don’t know how to begin Scripting this.
I’ve seen many topics around, but not the answer I’m looking for.
My idea to make this Crouch is to shortening the Capsule Collider instead of Shortening the whole thing which I guess is easy. And moving the Camera a little down. But I’m not very familiar with Resizing objects through Scripts.
And if there’s a better way to do it instead if Shortening the Collider please let me know.
Well its been awhile since I’ve done this but I would shorten the collider and change the camera height because you dont want the collider to be as large as the standing object and be crouching when a projectile hits the standing collider the player dies.
Another method is you could instance another collider while crouching and destroy the old one then when the player stands back up you could do the same for the standing player, however this could be more resource heavy and could reproduce unintended bugs .
This examines a previous case to see how to scale the controller collider, then how to modify the new sample assets beta FPC controller to include this functionality. If you take the time to understand the principles, you can apply them yourself to your own character controller.
@kingcharizard : Yeah, I thought of deleting the current collider and replacing with new, but that’s Pure Bullshit (No offence to Pure Bullshit). So I need to know how to do the other way.
@AlucardJay : I need to start everything from scratch, so I know what I’m doing. And That’s a lot to read and I hate it .
I did this a good while ago. Generally, there’s two approaches.
The first one is via scripting. You use Lerp on the collider height. However, since the collider’s height is relative to its origin (center), you’ll lose ground contact and fall to the floor all the time. Reducing the height by 1, “removes” 0.5 from the top and 0.5 from the bottom.
You’ll have to compensate for that, or your landing sound will cause problems.
It’s possible. In fact, I did it like this when writing the first iteration of my character controller.
However, it inflates your code and is rather inflexible.
A much cleaner approach is to take advantage of Mecanim for things like crouching and leaning.
Less code, more flexibility. You simply have a default state that has no changes and a crouched state where they height and position have been altered accordingly.
Then you just trigger the state change from within your code.
If you use a dedicated transition state, you can even alter how exactly your player crouches/stands up.
If you use a blendtree you can even control how much the player crouches down.
@TheSniperFan : Well, I’m really bad at Mecanim. TBH I don’t even know what it really is!
Well, this is my current script and it works almost fine. I guess you can point out to where it needs to be modified to make it more flexible with things.
Edit : Well got another problem. I’ve assinged shift button as Sprint button. In another script with Sprinting function I’ve added to play a sound when Sprinting. But when I just press the Sprint button the sound plays, it’s not even running. So how to make the sound play if only the Player is “Sprinting” not just when the sprint button is pressed. Is there a way to assign 2 buttons in the Input section like “left shift + Vertical” or something like?
Mecanim is Unity’s “new” animation system. You should learn it, since it will be extremely useful in many situations (new GUI system for example). Tutorials can be found here.
After watching the tutorials 1, 2, 9 and 10, you should be able to easily animate the height and position components of your collider and player.
You could also break this into two parts: collision and camera.
For the collision, have the normal collider and the short collider both in the prefab but on separate game objects. Only turn one of them on at a time (using .SetActive()). Essentially when you crouch you would instantaneously become shorter and be able to fit under things.
For the camera, you would have the script tween down the camera to the new lower height, and tween it back up when you’re done crouching.
I have played XBox games that do precisely this type of crouching, at least judging by how it feels to try and go underneath low-hanging objects when you press against them and tap crouch.
Speaking of that, if you are going to enable going under things, make sure you check overhead before you allow the player to uncrouch.
@TheSniperFan : Lol, so the “Part I Want” is done using Mechanim? And yeah please give me sample so I can see how it works.
@Kurt-Dekker : Well, as I remember I can’t add 2 colliders to the same object. And yeah the Player gets to crouch under vents and stuff. How do check overhead before standing??? I was gonna ask that.
To check overhead, you could have a simple trigger object that is always above your head (parented into your player’s structure on a separate game object), and when you are crouched, you start to use it to check if the area above you is clear and you can stand up.
Use a Capsule- or SphereCast for that.
I’ll quickly make an example using Mecanim for you and upload it here. I wrote a math exam yesterday, so I had no time for Unity stuff the last days.
EDIT:
Here you go.
I included a test scene. If you press C, the capsule will play a crouching animation. If you press C again, the capsule will stand up, if there’s enough clearance above. Try crouching and then moving the capsule underneath the second box. It won’t stand up unless you move the box high enough.
I added quite a bit of documentation to the code, so you can understand what is happening. Besides that, there’s some Debugging stuff, like a blue ray being drawn whenever the player can stand up, and a red one whenever he can’t.
If you were to remove all this stuff, you’ll notice how Mecanim enables you to do all this with an incredibly low amount of code.
The entire logic is basically 6 lines of code. This includes control structures like checking whether the button was pressed and caching the hash of the crouching state.