jshndr
January 5, 2023, 5:49am
1
Need help i cannot understand this jumping on sprite i dunno whats wrong on my code
heres my project
https://playcanvas.com/editor/scene/1620286
var Jump = pc.createScript('jump');
var force = 14; //You can change the value if you want.
// initialize code called once per entity
Jump.prototype.initialize = function() {
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};
// update code called every frame
Jump.prototype.update = function(dt) {
if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
this.entity.sprite.play("jump");
if (this.jumpTimer <= 0) {
this.jumpTimer = 0.5;
setTimeout(function(){
this.entity.rigidbody.applyImpulse(0, force, 0);
}.bind(this), 100);
}
}
else if (this.jumpTimer >= 0) {
this.jumpTimer -= dt;
}
else {
this.jumpTimer = 0;
}
};
Hi @jshndr ,
Are you getting any error? What’s your problem exactly?
Here is a simple implementation of jumping, there are several similar posts on the forum. You can try searching for that in case it helps:
You have to attach a script & make sure the player has a rigidbody & collision component attached to it. Make sure the rigidbody is type Dynamic. Then code a script similar to this:
//Variables
var force = 600; //You can change the value if you want.
NewScript.prototype.initialize = function() {
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};
NewScript.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
this.entity.rigidbody.app…
jshndr
January 8, 2023, 2:53am
3
I’m not sure if there is an error when the character is in flight, but the animation remains a run animation. I want to play the jump animation while jumping on another platform while in the air.
Hi @jshndr ! The problem is that you use two different scripts. One for movement and one for jumping. You can make one script of it or you have to communicate between the script, so both scripts know the player is jumping.
jshndr
January 8, 2023, 4:38am
5
Hi, I combined the two scripts, but it’s still the same.
var Player = pc.createScript('player');
var force = 10; //You can change the value if you want.
// initialize code called once per entity
Player.prototype.initialize = function(){
this.coin_Counter= 0;
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeydown, this);
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
var score = this.app.root.findByName("coinText");
score.element.text = this.coin_Counter.toString();
};
// update code called every frame
Player.prototype.update = function(dt) {
if(this.app.keyboard.isPressed(pc.KEY_D)){
this.entity.sprite.play("run");
this.entity.rigidbody.applyForce(4,0,0);
}else if(this.app.keyboard.wasReleased(pc.KEY_D)){
this.entity.sprite.play("idle");
}
if(this.app.keyboard.isPressed(pc.KEY_A)){
this.entity.sprite.play("brun");
this.entity.rigidbody.applyForce(-4,0,0);
}else if(this.app.keyboard.wasReleased(pc.KEY_A)){
this.entity.sprite.play("bidle");
}
if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
this.entity.sprite.play("jump");
if (this.jumpTimer <= 0) {
this.jumpTimer = 1;
setTimeout(function(){
this.entity.rigidbody.applyImpulse(0, force, 0);
}.bind(this), 100);
}
}
else if (this.jumpTimer >= 0) {
this.jumpTimer -= dt;
}
else {
this.jumpTimer = 0;
}
};
Player.prototype.onCollisionStart = function(result) {
if (result.other.name == "Goldcoin") {
result.other.destroy();
this.entity.sound.play('Pickup');
this.coin_Counter++;
var score = this.app.root.findByName("coinText");
score.element.text = this.coin_Counter.toString();
}
};
I changed your script a bit.
var Player = pc.createScript('player');
var force = 10; //You can change the value if you want.
// initialize code called once per entity
Player.prototype.initialize = function(){
this.onGround = true;
this.coin_Counter= 0;
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeydown, this);
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
var score = this.app.root.findByName("coinText");
score.element.text = this.coin_Counter.toString();
};
// update code called every frame
Player.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
if (this.onGround) {
this.onGround = false;
this.entity.sprite.play("jump");
this.entity.rigidbody.applyImpulse(0, force, 0);
}
}
else if(this.app.keyboard.isPressed(pc.KEY_D)){
this.entity.rigidbody.applyForce(4,0,0);
if (this.onGround) {
this.entity.sprite.play("run");
}
}
else if (this.app.keyboard.isPressed(pc.KEY_A)) {
this.entity.rigidbody.applyForce(-4,0,0);
if (this.onGround) {
this.entity.sprite.play("brun");
}
}
else if (this.onGround) {
this.entity.sprite.play("idle");
}
};
Player.prototype.onCollisionStart = function(result) {
this.onGround = true;
if (result.other.name == "Goldcoin") {
result.other.destroy();
this.entity.sound.play('Pickup');
this.coin_Counter++;
var score = this.app.root.findByName("coinText");
score.element.text = this.coin_Counter.toString();
}
};
1 Like