[SOLVED] Tank moving script

Hi, I’ve stated a project for a Tank game using this base: https://playcanvas.com/editor/scene/577508

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?

https://playcanvas.com/project/598511/overview/t54-model-test

last buil: https://playcanv.as/b/JKw8RMID/

Thanks

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);
    }
};
1 Like

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 ).

1 Like

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;
});

to :slight_smile:

//var Turretcontrols = pc.createScript('turretcontrols');

Turretcontrols.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.turret = this.entity.findByName("Tank_gun_turret");
    this.bullet = context.root.findByName("Bullet");
    this.barrel = context.root.findByName("Barrel");
};

Turretcontrols.prototype.update = function(dt) {
    var keyboard = this.app.keyboard;
    if (context.keyboard.isPressed(pc.input.KEY_A)) {
                this.turret.rotate(0, this.speed * dt, 0);
            } else if(context.keyboard.isPressed(pc.input.KEY_D)) {
                this.turret.rotate(0, -this.speed * dt, 0);
            }
            
            if (context.keyboard.wasPressed(pc.input.KEY_S)) {
                this.shoot();
            }
        
        
        
    var bullet = this.bullet.clone();
            context.root.addChild(bullet);
            bullet.setPosition(this.barrel.getPosition());
            bullet.enabled = true;
            
            this.force = new pc.Vec3();
            this.force.copy(this.turret.forward);
            this.force.scale(-this.impulse);
            
            bullet.rigidbody.applyImpulse(this.force);
    };

With a lot of errors.

The 2.0 version of turretcontrols.js is:

var TurretControls = pc.createScript('turretControls');

TurretControls.attributes.add('speed', { type: 'number', default: 10 });
TurretControls.attributes.add('impulse', { type: 'number', default: 25 });

// initialize code called once per entity
TurretControls.prototype.initialize = function() {
    this.force = new pc.Vec3();

    this.turret = this.entity.findByName("Tank_gun_turret");
    this.bullet = this.app.root.findByName("Bullet");
    this.barrel = this.app.root.findByName("Barrel");
};

// update code called every frame
TurretControls.prototype.update = function(dt) {
    var keyboard = this.app.keyboard;
    // Rotate the turret left and right using A and D
    if (keyboard.isPressed(pc.KEY_A)) {
        this.turret.rotate(0, this.speed * dt, 0);
    } else if(keyboard.isPressed(pc.KEY_D)) {
        this.turret.rotate(0, -this.speed * dt, 0);
    }
    
    if (keyboard.wasPressed(pc.KEY_S)) {
        this.shoot();
    }
};
        
TurretControls.prototype.shoot = function() {
    var bullet = this.bullet.clone();
    this.app.root.addChild(bullet);
    bullet.setPosition(this.barrel.getPosition());
    bullet.enabled = true;

    this.force = new pc.Vec3();
    this.force.copy(this.turret.forward);
    this.force.scale(-this.impulse);
    
    bullet.rigidbody.applyImpulse(this.force);
};

BTW, on the forum, to format a code block, place 4 back-tick symbols at the start and at the end of your code.

Thanks Will, now my T54 can move, rotate turret and fire :slight_smile: 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.

Cool! Well done on making good progress.

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 ) :slight_smile: , 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.

Last build https://playcanv.as/b/ClHjevFf/

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.

Thanks Will , now I need to learn the code :slight_smile: