[SOLVED] Add jumping to multiplayer example

Hello lads!
I am new to play canvas and want to create a multiplayer game, i want to add jumping to this example (PlayCanvas 3D HTML5 Game Engine) but I don’t know how. Please help
(ps: this is 4 a school project so please i need help fast)
-Cheers

Hi @cheerio_alpa and welcome,

There are some nicely written multiplayer tutorials here:

https://developer.playcanvas.com/en/tutorials/?tags=multiplayer

Thank you for responding so soon, But I have fallen in love with that one example. If worst comes to worst, I will look into those examples. If anyone who sees this could help, please.
-cheers

Also, I forgot to mention, I already have the multiplayer figured out i just need the jumping.

Hi @cheerio_alpa!

I have created an example project that maybe can help you.

https://playcanvas.com/project/1063800/overview/basic-jumping

1 Like

Thanks a bunch! i tried this but i could not figure out how to implement it into the existing code, Andy ideas?
-Cheers

The code I use is quite simple, so it should not be difficult to implant it somewhere in your existing movement code.

So thanks, but I’m dumb and when i inserted it at the bottom of the movement script and then run the game and try to press space, nothing happens :frowning:
-Cheers

You should place every part of code in the correct function. You probably already have an initialize and update function in your script. The jump function is a custom function that you can place at the end of your current script. There are also two attributes force and length, that should be placed almost on top of the script. In the project you can exactly see how I did it.

I tried doing what you had said, and it still did not work.

var PlayerMovement = pc.createScript('playerMovement');

PlayerMovement.attributes.add('speed', { type: 'number', default: 0.09 });

Jump.attributes.add('force', {
    title: 'Jump force',
    type: 'number',
    default: 5, 
});

Jump.attributes.add('length', {
    title: 'Sensor length',
    type: 'number',
    default: 1
});

PlayerMovement.prototype.initialize = function () {
    var app = this.app;
    var camera = app.root.findByName('Camera');
    this.cameraScript = camera.script.cameraMovement;
};

PlayerMovement.prototype.update = function (dt) {
    var app = this.app;

    var forward = this.entity.forward;
    var right = this.entity.right;

    var x = 0;
    var z = 0; 

    if (app.keyboard.isPressed(pc.KEY_A)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }

    if (x !== 0 || z !== 0) {
        var pos = new pc.Vec3(x * dt, 0, z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());

        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);

        this.entity.rigidbody.teleport(pos, rot);
    }
};





// initialize code called once per entity
Jump.prototype.initialize = function() {

};

// update code called every frame
Jump.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.jump();
    }
};

// initialize code called once per entity
Jump.prototype.jump = function() {
    var start = this.entity.getPosition();
    var end = new pc.Vec3(start.x, start.y - this.length, start.z);
    var ground = this.app.systems.rigidbody.raycastFirst(start, end);

    if (ground) {
        this.entity.rigidbody.applyImpulse(0, this.force, 0);
    }
};

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

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

Right now you have two initialize and update functions, while you only can have one of every function. Also every function has to start with the script name. In your case this is PlayerMovement and not Jump.

Unfortunately, I can’t explain all basics of scripting in this topic. Maybe the Crash Course below can help you.

https://developer.playcanvas.com/en/tutorials/crash-course/

Well, I’ve done everything I can think of nothing has worked. I hope someone can still help, I only have a few days left until this project is due. I would prefer to earn an b or hirer, without this help, it is likely that I cannot reach this goal.
-Cheers

anyone there?

If your script is correct now, you probably need to parse the script, because you have added some new attributes. Not sure if the script works correct if you don’t parse it first.

https://developer.playcanvas.com/en/user-manual/scripting/script-attributes/#getting-attributes-into-editor

I tryed and looked at this, it just broke the script :frowning:

That means your script is not correct. Can you share a link to the script?

Here, I have removed some attributes to try and reset it to how it was
[PlayCanvas | HTML5 Game Engine]

Are you sure you don’t removed to much? I suggest to compare it with the original example project.

Its allmost exact to the example now

Im not too sure on what your asking.