Wasd Help!

how is it now? https://playcanvas.com/editor/scene/1661669

@Jacob_Jack Move the camera under the box entity and then add camera instead of box

image

image

image

Do you want to move the box and the camera together? Or just move the camera?

Since my game is going to be in first person, I want the box and the camera moving together. https://playcanvas.com/editor/scene/1661669

How do I make my character jump?

Hi @Jacob_Jack!

There are a lot of topics on the forum about jumping. If that doesn’t help you, maybe my example project below can help.

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

I’m not working on the projectiles at the the moment, but it keeps saying “SyntaxError: Unexpected end of input”

var PProjectile = pc.createScript('pProjectile');

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

};
{entity.
// update code called every frame
PlayerProjectile.prototype.update = (function() 

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

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

Hi @Jacob_Jack! Line 7 is incorrect and should be removed. Line 10 has to be the same as line 6.

var PProjectile = pc.createScript('pProjectile');

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

};
// update code called every frame
PlayerProjectile.prototype.update = (function() 

}; 
// inherit your script state here
// Wasd.prototype.swap = function(old) { };

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

Is line 8 correct or should it start with PProjectile?

var PProjectile = pc.createScript('pProjectile');

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

};
// update code called every frame
PProjectile.prototype.update = (function() 

}; 
// inherit your script state here
// Wasd.prototype.swap = function(old) { };

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

The character is looking around. So far so good. https://launch.playcanvas.com/1661669?debug=true

you may try new First Person Controller from the Asset Store - download it from the Template filter. It has everything pre-set, just drop the controller into the scene after download

2 Likes

This one’s a school project I’ve been doing for last year and as well as now. There are several things wrong with the code but right now, I need help with the character model and jumping. For the scenes, make sure that you only use untitled.
https://playcanvas.com/editor/scene/1606628

var JumpInput = pc.createScript('jumpInput');

// initialize code called once per entity
JumpInput.prototype.initialize = function() {
    charaterMedium.app.keyboard.on(pc.EVENT_KEYDOWN, charaterMedium.keyDown, thcharaterMediumis);
    this.app.keyboard.on(pc.EVENT_KEYUP, this.keyUp, this);
};

// update code called every frame
JumpInput.prototype.keyDown = function (e) {
    if ((e.key === pc.KEY_J) && (this.charaterMedium.baseLayer.activeState !== 'jumpInput')) {
        this.charaterMedium.setBoolean('JumpInput', true);
    }
};

JumpInput.prototype.keyUp = function (e) {
    if ((e.key === pc.KEY_J) && (this.charaterMedium.baseLayer.activeState === 'jumpInput')) {
        this.charaterMedium.setBoolean('jumpInput', false);
    }
};

JumpInput.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.jump();
    }
};

// initialize code called once per entity
JumpInput.prototype.jump = function() {
    var start = this.entity.getPosition();
    var end = new pc.KEY_J(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/

Back to my personal game. I was about to send my code but I somehow messed it up.

https://playcanvas.com/editor/scene/1661669

ouse.enablePointerLock();
    }, this);            

    // Check for required components
    if (!this.entity.collision) {
        consovar Wasd = pc.createScript('wasd');

Wasd.attributes.add('camera', {
    type: 'entity',
    description: 'Optional, assign a camera entity, otherwise one is created'
});

Wasd.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: 'Adjusts the speed of player movement'
});

Wasd.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.25,
    description: 'Adjusts the sensitivity of looking'
});

// initialize code called once per entity
Wasd.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();
    
    var app = this.app;
    
    // Listen for mouse move events
    app.mouse.on("mousemove", this._onMouseMove, this);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
       app.mle.error("First Person Movement script needs to have a 'collision' component");
    }

    ,{if  (!this,entity,rigidbody , this,entity,rigidbody,type , pc,BODYTYPE_DYNAMIC) 
        console,error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
};

// update code called every frame
Wasd.prototype.update = function(dt) {
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {
        this._createCamera();
    }
    
    var force = this.force;
    var app = this.app;

    // Get camera directions to determine movement directions
    var forward = this.camera.forward;
    var right = this.camera.right;
       

    // movement
    var x = 0;
    var z = 0;

    // Use W-A-S-D keys to move player
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
        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;
    }

    // use direction from keypresses to apply a force to the character
    if (x !== 0 || z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};

Wasd.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }            
};

Wasd.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = 'Camera'();
    this.camera.setName("Camera");
    this.camera.addComponent("Camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};
var Wasd = pc.createScript('Camera');

Wasd.prototype.update = function (event) {
    if(this.app.keyboard.wasPressed(pc.KEY_SPACE) === true){
        this.entity.rigidbody.applyImpulse(0, 5, 0);
    }  
};

var Wasd = pc.createScript('jump');

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

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

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

};

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

// initialize code called once per entity
Wasd.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
// Wasd.prototype.swap = function(old) { };

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

Hey @Tirk182. Have you seen @yak32? For my school project, I’m trying to make my character move and run.

var JumpInput = pc.createScript('jumpInput');

// initialize code called once per entity
JumpInput.prototype.initialize = function() {
   if (charaterMedium.app.keyboard.on(pc.KEY_J, charaterMedium.keyDown, thcharaterMediumis)
    .app.keyboard.on(pc.KEY_J, this.keyUp, this));


// update code called every frame
JumpInput.prototype.keyDown = function (e) {
    if ((e.key === pc.KEY_J) && (this.charaterMedium.baseLayer.activeState !== 'jumpInput')) {
        this.charaterMedium.setBoolean('JumpInput', true());
    }
};

JumpInput.prototype.keyUp = function (e) {
    if ((e.key === pc.KEY_J) && (this.charaterMedium.baseLayer.activeState === 'jumpInput')) {
        this.charaterMedium.setBoolean('jumpInput', false());
    }
};

JumpInput.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_J)) {
        this.jump();
    }
};

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

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

JumpInput.prototype.update = function (event) {
    if(this.app.keyboard.wasPressed(pc.KEY_J) === true){
        this.entity.applyImpulse(0, 5, 0);
    }  
};

// Run when the E key is Pressed
    if (this.app.keyboard.isPressed(pc.KEY_E)) {
    this.entity.translate(-0.1, 0, 0); A
    }

if(app.keyboard.isPressed(pc.input.key.KEY_E)){
this.entity.translateLocal(-0.549,0,2.946)


    if (app.keyboard.isPressed(pc.KEY_A) || 0) {
        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;
    }

    // use direction from keypresses to apply a force to the character
    if (x !== 0 || z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};

JumpInput.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }
};

JumpInput.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = new pc.Entity();
    this.camera.setName("JumpInput");
    this.camera.addComponent("JumpInput");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 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/

@Jacob_Jack I am more than willing to help but what is the issue with your project? So I am not sure which project you are working on above. Can you provide the latest link to your project and let me know a few more details. Is this first person or third person? Maybe third person? If so did you have a look to this tutorial.
https://developer.playcanvas.com/en/tutorials/third-person-controller/

Please provide a little more detail so we can all help you with the issues you are seeing.

https://playcanvas.com/editor/scene/1606628

Third person

I am confused. Why isn’t the character moving?
https://playcanvas.com/editor/scene/1606628

var Jumpinput = pc.createScript('Jumpinput');

Jumpinput.attributes.add('movementSpeed', { type: 'number', default: 5 });
Jumpinput.attributes.add('runSpeedMultiplier', { type: 'number', default: 2 });

Jumpinput.prototype.initialize = function() {
    this.app.keyboard.on(pc.KEY_E, this.onShiftDown, this);
    this.app.keyboard.on(pc.KEY_E, this.onShiftUp, this);
};

Jumpinput.prototype.update = function(dt) {
    var movement = new pc.Vec3(0, 0, 0);

    if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
        movement.x -= 1;
    }

    if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
        movement.x += 1;
    }

    if (this.app.keyboard.isPressed(pc.KEY_UP)) {
        movement.z -= 1;
    }

    if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
        movement.z += 1;
    }

    movement.normalize();

    if (this.isRunning) {
        movement.scale(this.movementSpeed * this.runSpeedMultiplier * dt);
    } else {
        movement.scale(this.movementSpeed * dt);
    }

    this.entity.translate(movement);
};

Jumpinput.prototype.onShiftDown = function() {
    this.isRunning = true;
};

Jumpinput.prototype.onShiftUp = function() {
    this.isRunning = false;
};

Hi @Jacob_Jack!

It looks like you have renamed the script.

If you parse the script, you will see there is currently a problem with the script.

image

image

I suggest to remove the script from th entity and add it again.

The animations are not working. I Got the model and animations from Kenny

https://playcanvas.com/editor/scene/1606628