# Reseting Camera After Ending User Input/Adding Interpolation

**URL:** <https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259>\
**Category:** Help & Support\
**Created:** [June 19, 2017, 8:08pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259 "2017-06-19T20:08:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MobileFuse](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/m/f1d935/32.png) [@MobileFuse](https://forum.playcanvas.com/u/MobileFuse)\
**Post date:** [June 19, 2017, 8:08pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/1 "2017-06-19T20:08:15Z")

</div>

Hey everyone,

I’m trying to get the camera to reset to its initial position after the user finished interacting with it (orbiting around an object). I tried storing the initial position and rotation/euler angles on initialization and then setting those values at MOUSEUP. When I do this there is no reaction and the camera does not reset and just stays where the user leaves it. The following code is what I’m using to try and achieve this:

On initialize I have this:

```javascript
this.startPosition = this.entity.getPosition().clone();
this.startRotation = this.entity.getLocalEulerAngles().clone;

```

Then on mouse up I have this:

```javascript
this.entity.setLocalPosition(this.startPosition.x, this.startPosition.z, this.startPosition.y);
this.entity.setLocalEulerAngles(this.startRotation);

```

I tried referencing this thread but I had no luck with the solution: [[SOLVED] Reset position after impulse](https://forum.playcanvas.com/t/solved-reset-position-after-impulse/2090)

Here is the link to the project editor: [https://playcanvas.com/editor/scene/532716](https://playcanvas.com/editor/scene/532716)

Thanks,  
Diego

---

<div class="post-metadata">

**Author:** ![will](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/will/32/4560_2.png) [@will](https://forum.playcanvas.com/u/will)\
**Post date:** [June 19, 2017, 8:50pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/2 "2017-06-19T20:50:18Z")

</div>

That project is private so I get a 404 when I try to open it.

Why are you doing:

```javascript
this.entity.setLocalPosition(this.startPosition.x, this.startPosition.z, this.startPosition.y);
this.entity.setLocalEulerAngles(this.startRotation);

```

Instead of:

```javascript
this.entity.setPosition(this.startPosition);
this.entity.setEulerAngles(this.startRotation);

```

(And you’re getting local Eulers too. Essentially, I’m asking why you don’t just get/set world space position and rotation).

---

<div class="post-metadata">

**Author:** ![MobileFuse](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/m/f1d935/32.png) [@MobileFuse](https://forum.playcanvas.com/u/MobileFuse)\
**Post date:** [June 20, 2017, 12:43pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/3 "2017-06-20T12:43:29Z")

</div>

Hey Will,

I was trying out what you suggested first but it wasn’t working so I just went on to try different methods to see if it would help and the code I posted was the last thing I tried. I made the project public.

The code is in the mouse-input.js

---

<div class="post-metadata">

**Author:** ![will](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/will/32/4560_2.png) [@will](https://forum.playcanvas.com/u/will)\
**Post date:** [June 20, 2017, 8:49pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/4 "2017-06-20T20:49:39Z")

</div>

This is happening because your setting of the position and rotation of the camera is being overwritten in the orbit-camera.js script. In mouse-orbit.js, you have:

```javascript
MouseInput.prototype.initialize = function() {
    this.orbitCamera = this.entity.script.orbitCamera;

```

This is storing off a reference to the OrbitCamera script on the camera. The MouseInput script then just updates variables on the OrbitCamera script object in response to, say, a pan of the mouse:

```javascript
MouseInput.prototype.pan = function(screenPoint) {
    var fromWorldPoint = MouseInput.fromWorldPoint;
    var toWorldPoint = MouseInput.toWorldPoint;
    var worldDiff = MouseInput.worldDiff;
    
    // For panning to work at any zoom level, we use screen point to world projection
    // to work out how far we need to pan the pivotEntity in world space 
    var camera = this.entity.camera;
    var distance = this.orbitCamera.distance;
    
    camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
    camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);

    worldDiff.sub2(toWorldPoint, fromWorldPoint);
       
    this.orbitCamera.pivotPoint.add(worldDiff);    
};

```

At the end of that function, the `pivotPoint` property on the OrbitCamera script is updated. The function doesn’t directly update the position of the camera. That is done in the update function of the OrbitCamera script, called every frame:

```javascript
OrbitCamera.prototype.update = function(dt) {
    // Add inertia, if any
    var t = this.inertiaFactor === 0 ? 1 : Math.min(dt / this.inertiaFactor, 1);
    this._distance = pc.math.lerp(this._distance, this._targetDistance, t);
    this._yaw = pc.math.lerp(this._yaw, this._targetYaw, t);
    this._pitch = pc.math.lerp(this._pitch, this._targetPitch, t);

    this._updatePosition();
};

OrbitCamera.prototype._updatePosition = function () {
    // Work out the camera position based on the pivot point, pitch, yaw and distance
    this.entity.setLocalPosition(0,0,0);
    this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);

    var position = this.entity.getPosition();
    position.copy(this.entity.forward);
    position.scale(-this._distance);
    position.add(this.pivotPoint);
    this.entity.setPosition(position);
};

```

The solution? Store off `distance`, `pivotPoint`, `pitch`, and `yaw` properties on `this.entity.orbitCamera` instead and restore those values on the mouse up event.

---

<div class="post-metadata">

**Author:** ![MobileFuse](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/m/f1d935/32.png) [@MobileFuse](https://forum.playcanvas.com/u/MobileFuse)\
**Post date:** [June 22, 2017, 7:00pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/5 "2017-06-22T19:00:41Z")

</div>

Thanks will that did the trick.

---

<div class="post-metadata">

**Author:** ![MobileFuse](https://forum.playcanvas.com/letter_avatar_proxy/v4/letter/m/f1d935/32.png) [@MobileFuse](https://forum.playcanvas.com/u/MobileFuse)\
**Post date:** [June 27, 2017, 7:16pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/6 "2017-06-27T19:16:08Z")

</div>

Hey Will,

Currently I am trying to add some spherical interpolation to my camera when resetting to it’s starting position. I currently have it resetting successfully but I’m not sure how to have it slerp? I understand how to lerp a 3d object like a cube for example but I understand as well that the camera does not need to reset it’s position and rotation values but it’s yaw, pitch, and distance instead. I tried using some of the functions in the orbit-camera and mouse-input scripts but couldn’t find this functionality.

Project: [https://playcanvas.com/editor/scene/533551](https://playcanvas.com/editor/scene/533551)

Thanks,  
Diego

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [June 27, 2017, 7:59pm UTC](https://forum.playcanvas.com/t/reseting-camera-after-ending-user-input-adding-interpolation/4259/7 "2017-06-27T19:59:06Z")

</div>

There are a couple of ways I can think of doing this:

- Store the reset pitch, yaw, distance and pivotPoint and lerp the current values towards the reset values yourself in the input script.
- When you want to reset the camera, change the `orbitCamera.inertiaFactor` to a value like 0.1 or something similar and change it back to 0 when the user starts interacting again.
