Help with jump?

Hello, I am trying to make jumping for my game and I came up with this

 if(this.app.keyboard.wasPressed(pc.KEY_SPACE)) this.entity.rigidbody.applyImpulse(pc.Vec3.UP.scale( 10 /* the amount of power*/)

But when you go in launch there is no errors but I cannot jump.
Any ideas?
(Launch: https://launch.playcanvas.com/1105148 )

You forgot the Brackets

if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
    this.entity.rigidbody.applyImpulse(pc.Vec3.UP.scale( 10 /* the amount of power*/);
}
3 Likes

Thanks!

1 Like

Hm, Problems…

You miss a second bracket, try this for your initialize method:

Jumping.prototype.initialize = function(){
   if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
       this.entity.rigidbody.applyImpulse(pc.Vec3.UP.scale( 10 /* the amount of power*/));
   }
}
1 Like

Still :laughing:

Hello @Gabriel_Dobrzynski! Line 4 and 5 are the same. I suggest to remove line 4.

1 Like

Done. Still problems?

On line 6 you are missing a second ), and also the UP vector is an internal read only property, replace line 6 with this:

this.entity.rigidbody.applyImpulse(pc.Vec3.UP.clone().scale( 10 /* the amount of power*/));
1 Like

I still cannot jump? Launch: https://launch.playcanvas.com/1105148

To make this work you have to replace your current jump code to your update function and add the two lines below to your initialize function.

this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
2 Likes

@Gabriel_Dobrzynski developing a game in PlayCanvas requires you to have at least some knowledge in JavaScript programming. WIthout it, the experience will feel dounting and you will struggle to do even the basic things. I recommend brushing up your JS skills first and complete a good JS tutorial.

For example https://javascript.info/ looks good, althtough I haven’t personally tried it. You definitely want to complete the Part 1 and you can even do it inside PlayCanvas, without having to write HTML files.

Why do this:

this.entity.rigidbody.applyImpulse(pc.Vec3.UP.clone().scale( 10 /* the amount of power*/));

When you can do this:

this.entity.rigidbody.applyImpulse(0, 10, 0);

applyImpulse can also take 3 values, meaning you don’t have to mess around with vectors.

3 Likes

Thanks. Also @LeXXik I know enough javascript before starting I had to take a test online :smiley:

Edit: I know what to do

Hey all, I have fixed this problem added more onto it but when you go in launch without pressing anything you fly all the way up. What is happening? The new code I added:

this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this); 
    this.entity.rigidbody.applyImpulse(0, 5, 0);
}; 

Launch: https://launch.playcanvas.com/1105148

In this case it’s better to share your full code to see what’s wrong.

1 Like

Here it is :slight_smile:

var Jumping = pc.createScript('jumping');

// initialize code called once per entity
Jumping.prototype.initialize = function(){
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this); 
};
// update code called every frame
Jumping.prototype.update = function(dt) {
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this); 
   this.entity.rigidbody.applyImpulse(0, 5, 0);
};

// swap method called for script hot-reloading
// inherit your script state here
// Jumping.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/ 

You have to check your ‘if statement’ in your update function. This is not a correct if statement anymore.

2 Likes

I don’t have a ‘if statement’ in my script.