But all scripts ( tankcontrols.js, turretcontrols.js etc… ) not working, after a fast check I’ve see that code structure have been changed, now I’ve found a movement.js script, it work but not as expected, someone can help me to revert old 1.0 script to 2.0 for full tank control and firing?
Wow, that original tank project is now very old. Where did you find it?
Personally, I would avoid using projects that have v1.0 scripts as a starting point.
However, as a quick example, I can show you how to upgrade a v1.0 script to v2.0:
pc.script.create('tankcontrols', function (context) {
// Creates a new Tankcontrols instance
var Tankcontrols = function (entity) {
this.entity = entity;
this.force = new pc.Vec3();
this.torque = 0.5;
this.power = 10;
};
Tankcontrols.prototype = {
// Called once after all resources are loaded
initialize: function () {
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
if (context.keyboard.isPressed(pc.input.KEY_LEFT)) {
this.entity.rigidbody.applyTorque(0, this.torque, 0);
}
if (context.keyboard.isPressed(pc.input.KEY_RIGHT)) {
this.entity.rigidbody.applyTorque(0, -this.torque, 0);
}
if (context.keyboard.isPressed(pc.input.KEY_UP)) {
this.force.copy(this.entity.forward).scale(this.power);
this.entity.rigidbody.applyForce(this.force);
}
if (context.keyboard.isPressed(pc.input.KEY_DOWN)) {
this.force.copy(this.entity.forward).scale(-this.power);
this.entity.rigidbody.applyForce(this.force);
}
}
};
return Tankcontrols;
});
Would become:
var TankControls = pc.createScript('tankControls');
TankControls.prototype.initialize = function() {
this.force = new pc.Vec3();
this.torque = 0.5;
this.power = 10;
};
TankControls.prototype.update = function(dt) {
var keyboard = this.app.keyboard;
if (keyboard.isPressed(pc.KEY_LEFT)) {
this.entity.rigidbody.applyTorque(0, this.torque, 0);
}
if (keyboard.isPressed(pc.KEY_RIGHT)) {
this.entity.rigidbody.applyTorque(0, -this.torque, 0);
}
if (keyboard.isPressed(pc.KEY_UP)) {
this.force.copy(this.entity.forward).scale(this.power);
this.entity.rigidbody.applyForce(this.force);
}
if (keyboard.isPressed(pc.KEY_DOWN)) {
this.force.copy(this.entity.forward).scale(-this.power);
this.entity.rigidbody.applyForce(this.force);
}
};
Wow, thanks!!! Just tried and work well, now I will try to “upgrade” turretcontrol.js and bullet.js .
I’ve found the thank tutoria in google, there is a link to your blog with a connected and working project ( old ).
Hi, I’ve tried to modify turretcontrol.js … without success, from this:
//
pc.script.create('tankcontrols', function (context) {
// Creates a new Tankcontrols instance
var Tankcontrols = function (entity) {
this.entity = entity;
this.force = new pc.Vec3();
this.torque = 0.5;
this.power = 10;
};
Tankcontrols.prototype = {
// Called once after all resources are loaded
initialize: function () {
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
if (context.keyboard.isPressed(pc.input.KEY_LEFT)) {
this.entity.rigidbody.applyTorque(0, this.torque, 0);
}
if (context.keyboard.isPressed(pc.input.KEY_RIGHT)) {
this.entity.rigidbody.applyTorque(0, -this.torque, 0);
}
if (context.keyboard.isPressed(pc.input.KEY_UP)) {
this.force.copy(this.entity.forward).scale(this.power);
this.entity.rigidbody.applyForce(this.force);
}
if (context.keyboard.isPressed(pc.input.KEY_DOWN)) {
this.force.copy(this.entity.forward).scale(-this.power);
this.entity.rigidbody.applyForce(this.force);
}
}
};
return Tankcontrols;
});
Thanks Will, now my T54 can move, rotate turret and fire https://playcanv.as/p/IFlDasMb/
Can I ask you also camera.js and bullet.js ( I did not understand if it is used, apparently the tank shoots even with the old code ) to complete the first step of project?
Thanks again.
Can I ask you also camera.js and bullet.js ( I did not understand if it is used, apparently the tank shoots even with the old code ) to complete the first step of project?
Sorry, what exactly are you asking here? Apologies for not understanding…
Hi, in the “old” tank project I’ve found 4 scripts : tankcontrol.js turretcontrol.js camera.js and bullet.js , I’ve modified with your help the firs two, infact the project works, I have make the camera as child of turret and it works well ( like war thunder tank view ) , the gun also works… Now, camera.js is not utilized I can delete it, bullet.js too? Is bullet.js used or ignored by project?
Thanks.
Yeah, camera.js and bullet.js were both used in the original ‘playhack-nov’ project that you forked.
camere.js implements a ‘follow camera’ that smoothly interpolated behind the tank (whereas your camera is locked at a fixed offset behind the tank).
bullet.js is essentially for destroying a bullet after 20 seconds in flight. If you don’t destroy the bullets, they’ll stay in your scene forever and accumulate over time, slowing things down.