Hi, I’m very new to scripting and I’m trying to switch between two characters when x is pressed. The two characters I am switching between are the drill and the stickman. If someone could send a script or fork my game and create a script it would be much appreciated.
@John_Delbaere Hi and welcome to Playcanvas. Here are a few resources we have to get you going. First the key controls can be reference here.
https://developer.playcanvas.com/en/tutorials/keyboard-input/
There are several examples that use keyboard keys in this section of tutorials.
https://developer.playcanvas.com/en/tutorials/
What is nice about the tutorials is that you can fork any one of the examples and make them your own for adding or changing scripts.
Thank you so much!
Just a follow up question, I wrote this script
var Stickmanmovement = pc.createScript('stickmanmovement');
// initialize code called once per entity
Stickmanmovement.prototype.initialize = function() {
};
// update code called every frame
Stickmanmovement.prototype.update = function(dt) {
this.playerPos = this.entity.getLocalPosition();
var app = this.app;
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
this.entity.rigidbody.applyTorque (4,0,0);
}
if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
this.entity.rigidbody.applyTorque (1,0,0);
}
if (this.app.keyboard.isPressed(pc.KEY_UP)) {
this.entity.rigidbody.applyForce(0,0,10);
}
if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
this.entity.rigidbody.applyForce(0,0,10);
}
if (app.keyboard.wasPressed(pc.KEY_R) ) {
this.reset();
}
};
Stickmanmovement.prototype.reset = function () {
this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
};
but when I press the arrow keys nothing seems to happen. I added it into the stickman and it has all the components needed. If you could tell me what I need to fix it would be much appreciated. By the way here is my project game.
@John_Delbaere I took a look at you project link above and I am sorry but I do not see this code.
Sorry, I think I sent the wrong link. This should work https://playcanvas.com/project/1061106/overview/mining-mania
I’ve made some changes to the code in order to play animations but it seems those are also working incorrectly.
@John_Delbaere Your keys are working. I just put in a few console.log()s to capture the key strokes in the debugger. Also, there were some semi colons missing. Here is the code.
var Stickmanmovement = pc.createScript('stickmanmovement');
Stickmanmovement.attributes.add('modelEntity', {
type: 'entity'
});
// initialize code called once per entity
Stickmanmovement.prototype.initialize = function() {
};
// update code called every frame
Stickmanmovement.prototype.update = function(dt) {
this.playerPos = this.entity.getLocalPosition();
var force = this.force;
var app = this.app;
// movement
var forward = this.modelEntity.forward;
var x = 0;
var z = 0;
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
console.log('KEY_LEFT_is');
this.entity.rigidbody.applyTorque (4,0,0);
}
if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
console.log('KEY_RIGHT_is');
this.entity.rigidbody.applyTorque (1,0,0);
}
if (this.app.keyboard.isPressed(pc.KEY_UP)) {
console.log('KEY_UP_is');
x += forward.x;
z += forward.z;
this.entity.anim.setTrigger('Walking Forward');
}
if(this.app.keyboard.wasReleased(pc.KEY_UP)){
console.log('KEY_UP_was');
this.entity.anim.setTrigger('Idle');
}
if (this.app.keyboard.isPressed(pc.KEY_DOWN)) {
console.log('KEY_DOWN_is');
x -= forward.x;
z -= forward.z;
this.entity.anim.setTrigger('Walking Backward');
}
if(this.app.keyboard.wasReleased(pc.KEY_DOWN)) {
console.log('KEY_DOWN_was');
this.entity.anim.setTrigger('Idle');
}
if (app.keyboard.wasPressed(pc.KEY_R) ) {
this.reset();
}
if (x !== 0 && z !== 0) {
force.set(x, 0, z).normalize().scale(this.power);
this.entity.rigidbody.applyForce(force);
}
};
Stickmanmovement.prototype.reset = function () {
this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
};
Output in debugger
I am not sure about what you really want to do in your position updates. You should probably have another look and follow it through. Please post a new topic referencing any new issue. We should probably mark this as solved since it does not match the topic of this post.