First Person Movement Crouching

Hello all

I’m new to playcanvas and programming i started with the first person movement script to create a fps game but could not figure out how to implement crouching. I did some research in unity and the forum and found out that reducing the collider to half its original value should work but when I implement this myself everything seems to be really slow. The player moves really slowly in general and when you crouch it crouches really slowly and hardly moves, and the crouch bugs through the floor as well.

https://playcanvas.com/editor/scene/1191042

Here is the code that I put for crouching

if (app.keyboard.isPressed(pc.KEY_C)) {
        this.entity.collision.height = 1;
    }
    else {
        this.entity.collision.height = 2;
    }

Could somebody please take a look at the code and tell me what I did wrong or point me in the correct direction?

Thanks

Hi @nasjarta and welcome,

I think when changing a collision component property to have the physics body get updated you need to trigger somehow the physics system to recreate the rigid shape and body. Easiest way is by toggling on/off the rigidbody component.

Here is my try on your code with crouching being a toggle:

    if (app.keyboard.wasReleased(pc.KEY_C)) {
        
        if( !this.crouching){
            this.crouching = true;
            
            this.entity.collision.height = 1;

            this.entity.rigidbody.enabled = false;
            this.entity.rigidbody.enabled = true;            
            
        }else{
            this.crouching = false;
            
            this.entity.collision.height = 2;

            this.entity.rigidbody.enabled = false;
            this.entity.rigidbody.enabled = true;               
        }
    }

3 Likes

Hi @Leonidas

Thank you so much for your help, this works perfect I did a tiny modification and made it so you can push to hold crouch:

if (app.keyboard.isPressed(pc.KEY_C)) {
        if (!this.crouching) {
            this.crouching = true;
            
            this.entity.collision.height = 1;
            
            this.entity.rigidbody.enabled = false;
            this.entity.rigidbody.enabled = true;
        }
    } else if (app.keyboard.wasReleased(pc.KEY_C)) {
        this.crouching = false;
        
        this.entity.collision.height = 2;
        
        this.entity.rigidbody.enabled = false;
        this.entity.rigidbody.enabled = true;
    }

One other question I have is, would you by any chance know how to make the transition from standing to and then crouch to standing smoothly? I tried to do some practice with vec3.lerp but could not find a solution to achieve this. I don’t think I am good enough to figure it out somehow even though I checked unity and the played around with the lerp method. Could you please point me to the correct direction in achieving this? Is it possible to do without an animation of some sort.

Thanks

1 Like

Im pretty sure you would want to only lerp the camera, while the physics apply instantly. Send me a message if you need help. I will try to make a working example in the mean time.

2 Likes

@pixelpros can you help me?

Done! Its a mess but works really well. Lmk if you have any questions.
https://playcanvas.com/project/811438/overview/fps-crouch-physics

Threw in a raycast system that checks above player when crouching.
image

4 Likes

Nice! Thanks for sharing

@pixelpros Thank you so much your project is really good and also really helpful. I am going to study your code as a beginner and want to say what you did is really inspirational for me. I am going to implement a jump script by myself as challenge and try to see if I can master raycasts for this :smiley:

Dumb question: I replicated your project but did I do something wrong in my version because the player model clips through the camera every time I crouch and uncrouch.

@nasjarta No problem, the raycast system I implemented is a bit complex, you could do a single raycast down for jump and it should suffice.

I updated it so you can smooth the player model scaling. But if you went with skeletal animations later, you wont need that.

I saw your second example just now with the 2 separate collision states for standing and crouching. This feels really smooth and the model doesn’t clip through the camera when you crouch/uncrouch which is really awesome. Can I also ask why the model was clipping through the camera in the first script? I can’t seem to understand why it was doing that.

I updated it so you can smooth the player model scaling. But if you went with skeletal animations later, you wont need that.

So should I use the first script only if I want to use skeletal animations later or should I use your V2 script from now on? Sorry English is not my first language I may have not understood that correctly.

You can use either for skeletal animations, second version is improved by using 2 collision bodies, without the need to teleport the rigidbody entity.

The problem is that the player model was scaling instantly, while the camera was interpolated up and down. See example below, red crosshair is approximation of camera position smoothed.

3 Likes

This makes perfect sense. Thanks for all your help!