- Home /
Problems with crouching (FPS/C#)
I'm having a bit of a problem, I'm trying to add a crouch function to my game, I have the animation and camera position working perfectly, but the problem is the collider.
I'm trying to reduce the collider size and position, but when I do that, every time I go to crouch, then back up, I always go right through the floor.
Here's my code:
private bool crouching;
public float crouchColliderHeight;
public float crouchColliderYPosition;
private float currentColliderHeight;
private float currentColliderYPosition;
private CharacterController characterCollision;
void Start ()
{
currentColliderHeight = characterCollision.height;
currentColliderYPosition = characterCollision.center.y;
}
void Update ()
{
crouching = Input.GetButton("Crouch");
if (crouching)
{
characterCollision.height = crouchColliderHeight;
characterCollision.center = new Vector3(characterCollision.center.x, crouchColliderYPosition, characterCollision.center.z);
}
else
{
if (characterCollision.height != currentColliderHeight)
{
float previousGravity = gravity;
characterCollision.height = currentColliderHeight;
gravity = 0;
characterCollision.center = new Vector3(characterCollision.center.x, currentColliderYPosition, characterCollision.center.z);
gravity = previousGravity;
}
}
}
What am I doing wrong?
Also, is there anyway to make it so that if the crouch collider is hitting something above it, it will stay crouching even after you let go of the crouch key?
Unfortunately I can't tell what your first problem is about. But I got an approach to your second problem. You might get this to work if you add another collider to your character asset. You can make this collider a box or capsule collider, and make it the same size as the character controller, but make it trigger ins$$anonymous$$d of a normal collider. Then, when crouched, you'll be able to check if that trigger is actually colliding with something, that would tell you that your character isn't able to stand up at that point, so he should stay crouched
Once I get the first problem fixed, I could definetly give that solution a try, it might just work, thanks!
Answer by Gizmoi · Jan 28, 2013 at 10:23 PM
Edit: Moved comment to answer:
I think what's happening is that you're shrinking the collider, thus the player drops to a crouch height, then when you expand the collider again you are intersecting the floor, so physics just assumes you've gone too far and drops you. Try translating the player up by the change in collider height.
Also, for your second problem, probably best to make use of triggers in a different way. Decide on the areas where you must remain crouched and place a box collider around that area, then tell your player upon entering and remaining in that collider he must be crouched. This way he will not attempt to stand for a frame, then hit a trigger and find out he must then crouch again, he will simply stay crouched.
I've tried the first solution, as you can see in the code, but the player seems to go through the floor regardless. Unless I'm doing it wrong?
Have you had a look in the scene view with the collider selected so that you can see what happens to it as you crouch and un-crouch?
Where are you setting crouchYPosition and crouchColliderHeight?
Also I wouldn't bother with the gravity, you're essentially disabling and enabling it in the same frame, which should have no effect.
I've had a look as as far as I can see, the position of the bottom both colliders are the same, they're just different heights.
I'm setting the crouch height and y position using the inspector, because the game I'm making will have multiple characters, and they're all different heights.
Oh! Hold on! I just solved it! All I did was set the crouchcolliderYPosition down a bit, so it wouldn't go through the floor, and it worked!
This post helped solve my similar problem. I include a timer because the game is for dodging obstacles by jumping/crouching, the player will automatically stand back up after a set time.
My code:
public class PlayerController : MonoBehaviour
{
private BoxCollider playerBC;
private float crouchTime = 1f;
private float timer;
private Vector3 boxInitCenter;
private Vector3 boxInitSize;
void Start()
{
playerBC = GetComponent<BoxCollider>();
boxInitCenter = playerBC.center;
boxInitSize = playerBC.size;
}
void Update()
{
//for crouching
if (Input.GetKeyDown(KeyCode.LeftControl) && isGrounded && !gameOver)
{
timer = Time.time;
playerBC.size = new Vector3 (playerBC.size.x, 2.23f, playerBC.size.z);
playerBC.center = new Vector3 (playerBC.center.x, 1.24f, playerBC.center.z);
}
//for standing after crouching
if (Time.time > timer + crouchTime)
{
playerBC.size = boxInitSize;
playerBC.center = boxInitCenter;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Relationship between Events and Update() 2 Answers
Instantiating Prefabs through Editor Script 1 Answer
Character cannot move Diagonally forwards 0 Answers
Trouble with Iterative Coroutine 1 Answer