How to get entity backwards like entity.forward does

I have a turret on a ship and want to apply impulse on fire events. All set to entity forward position except need to reverse it. Searched on the entity API page, forums and google but couldn`t find anything useful or working.

            var turretForward    = this.turret.forward;
            //Gonna apply the backward code one following variable
            var turretBack    = this.turret.forward;

            point    = this.turret.script.turret.resultPoint;
            var distance = Math.min(Math.max(point.sub(position).length(), 7.5), 20);

            this.force = new pc.Vec3();
            this.force.scale(distance * 2, distance * 3);
            this.forceBack = new pc.Vec3();
            this.forceBack.scale(distance / 2, distance / 3);
            firedBall.rigidbody.applyImpulse(this.force.x, this.force.y + (distance / 25), this.force.z);
            //This works but to the forward. I would like to change into reverse version of forward
            this.entity.rigidbody.applyImpulse(this.forceBack.x, this.forceBack.y + (distance / 25), this.forceBack.z);

Could link to the project if necessary.

Hey @theronax and welcome!

Try chaning the line above with this one:

var turretBack    = this.turret.forward.clone().scale(-1);

Normally you shouldn’t be cloning a vector per frame, to avoid the performance hit. So when you get it working you can start using a temp vec, created on init time, to copy/scale the forward vector.

Noted! So by not per frame but init time, you mean the initialize function rather than update function, right?

Thanks so much (I composed this reply but it stuck on sending I guess, resending now)

Yeah, something like this:

MyScript.prototype.initialize = function(){
   this.vec = new pc.Vec3();

MyScript.prototype.update = function(dt){
   var turretBack = this.vec;

Doing this will avoid allocating new pc.Vec3 objects every frame. Objects that the garbage collector has to struggle to clean up.