Hello! I am here seeking advice once again on coding. I want to implement a crouching mechanic in my game, however, whenever I launch to playtest, I keep getting an error. Could someone please assist me.
Code of Interest Below:
if(this.keyboard.isPressed(pc.KEY_SHIFT))
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;
}
Error Received: Cannot read properties of undefined (reading ‘isPressed’)
Hi @Overbaked_Prod,
try updating your if statement to:
this.app.keyboard.isPressed(pc.KEY_SHIFT)
The app object is where you can find the keyboard object and its various methods.
Check out this part of the user manual for more examples:
https://developer.playcanvas.com/en/tutorials/keyboard-input/
2 Likes
Hello, I used the code you suggested below and now get this error.
Cannot read properties of undefined (reading ‘keyboard’)
I am completely stumped on how to figure this out as the if statement is formated same as everything else in the code.
Hi @Overbaked_Prod! Can you share your project or script please?
Here is the entirety of the script, my good sir.
var Heyehyey = pc.createScript('heyehyey');
// initialize code called once per entity
Heyehyey.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
Heyehyey.prototype.update = function(dt) {
if(this.app.keyboard.isPressed(pc.KEY_D)){
this.entity.rigidbody.applyForce(0,0,5000);}
if(this.app.keyboard.isPressed(pc.KEY_A)){
this.entity.rigidbody.applyForce(0,0,-2000);}
//Jumping
var canJump = false;
var from = this.entity.getPosition();
var to = new pc.Vec3(from.x, from.y - this.entity.getLocalScale().y, from.z);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
var toLeftSide = new pc.Vec3(from.x - this.entity.getLocalScale().x, from.y, from.z);
var resultLeftSide = this.app.systems.rigidbody.raycastFirst(from, toLeftSide);
var toRightSide = new pc.Vec3(from.x + this.entity.getLocalScale().x, from.y, from.z);
var resultRightSide = this.app.systems.rigidbody.raycastFirst(from, toRightSide);
var airborne = false;
var dash = this.app.keyboard.wasPressed(pc.KEY_SPACE);
var jump = this.app.keyboard.wasPressed(pc.KEY_W);
var onGround = false;
var crouching =false;
Heyehyey.prototype.onCollisionStart = function(result){
if(result.other.tags.has('ground')){
onGround = true;
}
else
{
onGround = false;
}
};
if(result || resultLeftSide || resultRightSide)
{
canJump = true;
airborne = false;
}
else
{
canJump = false;
airborne = true;
}
if(this.app.keyboard.wasPressed(pc.KEY_W) && canJump)
{
if(resultLeftSide && !result)
this.entity.rigidbody.applyImpulse(0,1500,1000);
else if(resultRightSide && !result)
this.entity.rigidbody.applyImpulse(0,1500,-1000);
else if(result)
this.entity.rigidbody.applyImpulse(0,1500,0);
}
};
this.app.keyboard.isPressed(pc.KEY_SHIFT);
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;
}
You have some code outside the update function and you have your collision start function inside the update function. In the script below I fixed some problems that I have found.
var Heyehyey = pc.createScript('heyehyey');
// initialize code called once per entity
Heyehyey.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// UPDATE FUNCTION
// update code called every frame
Heyehyey.prototype.update = function(dt) {
if(this.app.keyboard.isPressed(pc.KEY_D)){
this.entity.rigidbody.applyForce(0,0,5000);}
if(this.app.keyboard.isPressed(pc.KEY_A)){
this.entity.rigidbody.applyForce(0,0,-2000);}
//Jumping
var canJump = false;
var from = this.entity.getPosition();
var to = new pc.Vec3(from.x, from.y - this.entity.getLocalScale().y, from.z);
var result = this.app.systems.rigidbody.raycastFirst(from, to);
var toLeftSide = new pc.Vec3(from.x - this.entity.getLocalScale().x, from.y, from.z);
var resultLeftSide = this.app.systems.rigidbody.raycastFirst(from, toLeftSide);
var toRightSide = new pc.Vec3(from.x + this.entity.getLocalScale().x, from.y, from.z);
var resultRightSide = this.app.systems.rigidbody.raycastFirst(from, toRightSide);
var airborne = false;
var dash = this.app.keyboard.wasPressed(pc.KEY_SPACE);
var jump = this.app.keyboard.wasPressed(pc.KEY_W);
var onGround = false;
var crouching =false;
if(result || resultLeftSide || resultRightSide)
{
canJump = true;
airborne = false;
}
else
{
canJump = false;
airborne = true;
}
if(this.app.keyboard.wasPressed(pc.KEY_W) && canJump)
{
if(resultLeftSide && !result)
this.entity.rigidbody.applyImpulse(0,1500,1000);
else if(resultRightSide && !result)
this.entity.rigidbody.applyImpulse(0,1500,-1000);
else if(result)
this.entity.rigidbody.applyImpulse(0,1500,0);
}
if (this.app.keyboard.isPressed(pc.KEY_SHIFT)) {
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;
}
}
// END FUNCTION
};
// COLLISION START FUNCTION
Heyehyey.prototype.onCollisionStart = function(result){
if(result.other.tags.has('ground')){
onGround = true;
}
else
{
onGround = false;
}
// END FUNCTION
};