I am trying to make it so that when the player is holding down the space bar the camera moves down and the player’s collision shrinks and moves down to allow the player to move through smaller gaps, but I cannot figure out how to change the size or the collision offset for a capsule type collision. does anyone know how to do it?
Some info you might find useful:
thanks, but when I tried to do it with hight It gave me an error
Hi @Linkplayer!
It will be helpful if you share the error and how you try to do it.
I got it working, it is supposed to be this.entity.collision.hight = ([variable value])
the error I had was I thought it was this.entity.collision.hight([variable value])
Thanks for sharing.
// height of collision component
this.entity.collision.height = 2;
// radius of collision component
this.entity.collision.radius = 1;
However I have another problem, the script is for the player crouching, but what I have seen with crouching the mechanic to detect if they can stop crouching is with a ray cast to detect if there is a ceiling, but my game there is always a ceiling, is there a way to use collisions to find if the player can stand or not?
Hmm, I think I would use a raycast for this to detect a ceiling above the player.
How do you currently detect the player need to crouch?
but the problem is that the ray cast will always hit the ceiling everywhere in the game causing the character to be crouching eternally
You can limit the height of the raycast or without limiting the height you can use the hit distance to check the height of the ceiling.
OK, but I cannot figure out how to use Raycast at all, Can I have the script for this situation?
You can add the sample code below in for example the update function of your movement script. For this code you need to add a child entity to your player entity with the name Raycast
. Place this enity above the player. When the raycast hit something between the player entity and this entity there will be a result
.
var start = this.entity.getPosition();
var end = this.entity.findByName('Raycast').getPosition();
var result = this.app.systems.rigidbody.raycastFirst(start, end);
if (result) {
// start crouch
}
else {
// stop crouch
}
ok, but I tried to do that, but when it went to the if (result) part, it says false
Can you share the editor link of your project?
https://playcanvas.com/editor/scene/1882911
this is in my testing scene
Where did you apply the code? I also can’t find the entity with the name ‘Raycast’…
the capsule has a script called crouch with the data I coded.
also, I renamed the find by name to New entity because that is what I had the entity be called, I just changed both to Raycast
Currently I don’t understand exactly how your script exactly works, but I will debug your project when I’m home.
You can add some logs to see which part of your script is failing. Open the console of your browser with the F12 key to see the logs. Below two examples.
console.log('Check 1');
console.log(this.entity.enabled);
I put the crouching script into the 3D movement script and it works perfectly
um, the crouching script keeps destroying and replacing the collision every frame which causes the FPS script to keep resetting the speed, so I removed it for my game, so instead I will add it to a game where you play as a 3D modeled charachter