I’m pretty new to play canvas and i just started learning the mechanics. I’m having trouble with creating a script that allows the player to jump using space bar.
Hi @BleachDev and welcome,
Here is a simple script that will allow the player to jump using physics:
var Jump = pc.createScript('jump');
Jump.prototype.update = function (event) {
if(this.app.keyboard.wasPressed(pc.KEY_SPACE) === true){
this.entity.rigidbody.applyImpulse(0, 5, 0);
}
};
Make sure that the entity you attach this has a collision and rigidbody component attached.
I tried implimenting it, changing the mass of my character and other things, but it’s not working. Is it because I have a first person camera controls?
Wait never mind I think i figured it out
when i press space, it teleports me back to my start
hey um do i just have to copy the script and paste it on the player i want to jump or do i have to make custom script?
Well, it’s a very simple script, you can try and adding it on an entity that has a rigidbody component and see for yourself if it works or not.
No, that seems quite correct.
im using it for the first person game im making does it not work in first person?
The script doesn’t have any requirement, if you check the code it will only add an impulse on the Y axis to the body you attach it to.
Did you attach it to an entity in editor?
how do i do that?
You can select an entity and use the inspector panel in the editor to add a script component.
From there you can click and find your jump.js script and add it to your entity.
https://developer.playcanvas.com/en/user-manual/designer/inspector/
could you send a screenshot of what im looking for
- Select an entity and to the right click Add Component and add a script component.
- Then click on the empty input field that appeared together with the script component and find your jump script:
Try increasing the impulse value to something bigger, like 100 or 1000, the body has a big mass.
If that doesn’t work, try posting a sample project to take a look why it doesn’t work for you.
https://playcanvas.com/project/770644/overview/my-project-copy
That was a copy of what I did. I did everything that u told the other person to do but it’s not working for me. Please help as this is the first game that I am creating. Thanks!
Hey @Gameinator, I just had a quick look at your project, the jumping script is empty?
That’s weird maybe try again right now. Any time I click the file it shows the code?