# How to get entity backwards like entity.forward does

**URL:** <https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103>\
**Category:** Help & Support\
**Created:** [February 14, 2020, 1:57pm UTC](https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103 "2020-02-14T13:57:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theronax](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/theronax/32/7186_2.png) [@theronax](https://forum.playcanvas.com/u/theronax)\
**Post date:** [February 14, 2020, 1:57pm UTC](https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103/1 "2020-02-14T13:57:25Z")

</div>

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.

```javascript
            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.copy(turretForward);
            this.force.scale(distance * 2, distance * 3);
            
            this.forceBack = new pc.Vec3();
            this.forceBack.copy(turretBack);
            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.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [February 14, 2020, 2:05pm UTC](https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103/2 "2020-02-14T14:05:27Z")

</div>

> [@theronax](#):
>
> var turretBack = this.turret.forward;

Hey @theronax and welcome!

Try chaning the line above with this one:

```javascript
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.

---

<div class="post-metadata">

**Author:** ![theronax](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/theronax/32/7186_2.png) [@theronax](https://forum.playcanvas.com/u/theronax)\
**Post date:** [February 25, 2020, 8:50pm UTC](https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103/3 "2020-02-25T20:50:23Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [February 25, 2020, 10:46pm UTC](https://forum.playcanvas.com/t/how-to-get-entity-backwards-like-entity-forward-does/12103/4 "2020-02-25T22:46:14Z")

</div>

Yeah, something like this:

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

MyScript.prototype.update = function(dt){
   this.vec.copy(this.turret.forward).scale(-1);
   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.
