# How do i change orbitCamera distance from another script

**URL:** <https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734>\
**Category:** Help & Support\
**Tags:** scripting\
**Created:** [May 16, 2022, 8:44am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734 "2022-05-16T08:44:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 16, 2022, 8:44am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/1 "2022-05-16T08:44:20Z")

</div>

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/e/5/e5437f3179861323da4558fb22248a49bdc4b45f.png)  
So what I’m going to do is in the multi-user case, one user zooms in on the camera and another user can see it. The socket I am using can pass the distance, but how can I change the orbitCamera distance once I receive it?  
I used “this.app.root.findByName(‘Camera’).script.orbitCamera;”  
But I can’t get distance in orbitCamera  
It show undefind

---

<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:** [May 16, 2022, 8:58am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/2 "2022-05-16T08:58:22Z")

</div>

> [@leooooooo](#):
>
> I used “this.app.root.findByName(‘Camera’).script.orbitCamera;”

That should be fine as long as there is only one Entity called Camera in the scene. Can you share the project with this issue please?

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 5:40am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/3 "2022-05-17T05:40:17Z")

</div>

sorry， It’s a private project  
I can’t share the project  
Now I have another problem  
I don’t know how to use Object.defineProperty

---

<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:** [May 17, 2022, 6:45am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/4 "2022-05-17T06:45:17Z")

</div>

Unfortunately, its difficult to help without a more complete example of the issue you are having.

Your original issue is mostly likely due to having multiple cameras or entities in the scene named the same (‘Camera’).

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 7:00am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/5 "2022-05-17T07:00:37Z")

</div>

Do you know how to change orbitCamera distance from other scripts?

 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/2/7/272d25b1952084d782e0a673b124aa950bbba8e2.png)

---

<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:** [May 17, 2022, 7:05am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/6 "2022-05-17T07:05:00Z")

</div>

Yes, there are examples done with the input scripts in the orbit camera project.

[https://playcanvas.com/editor/code/438243?tabs=6861120](https://playcanvas.com/editor/code/438243?tabs=6861120)

However, the problem is with your specific project so unless you can share a public repro of the issue, its really difficult to help.

---

<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:** [May 17, 2022, 7:15am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/7 "2022-05-17T07:15:25Z")

</div>

You can fork the project and remove all the private assets etc to share the issue publicly. We just need to see the relevant code and scene hierarchy

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 7:25am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/8 "2022-05-17T07:25:02Z")

</div>

I have to apply for permission if I can fork

---

<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:** [May 17, 2022, 7:41am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/9 "2022-05-17T07:41:58Z")

</div>

> [@leooooooo](#):
>
> It show undefind

Can you show the full error message please?

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 8:04am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/10 "2022-05-17T08:04:38Z")

</div>

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/e/9/e91c8a4ebb8af7029c501e59d31e18b51ca73d5c.png)  
 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/b/f/bf7846299ed2f211d13d512a86b4c27b9ab84eb3.png)

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/a/9/a9a712230765ead5da2053294ecebbfee687b52e.png)

---

<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:** [May 17, 2022, 8:10am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/12 "2022-05-17T08:10:34Z")

</div>

If you search for ‘Camera’ in the hierarchy view in the Editor, how many entities does it find?

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 8:11am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/13 "2022-05-17T08:11:33Z")

</div>

only one

---

<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:** [May 17, 2022, 8:12am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/14 "2022-05-17T08:12:32Z")

</div>

In which case, a repro project of the issue needs to be shared. The code looks fine, it is likely to be a scene setup issue

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 8:12am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/15 "2022-05-17T08:12:50Z")

</div>

two scripts are not on the same entity  
main.js and orbitCamera

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 8:16am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/16 "2022-05-17T08:16:36Z")

</div>

I prepare to remove all the private assets

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [May 17, 2022, 8:41am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/17 "2022-05-17T08:41:16Z")

</div>

I see you use `pc.app.root` instead of `this.app.root`. Are you sure this is correct?

---

<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:** [May 17, 2022, 8:42am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/18 "2022-05-17T08:42:16Z")

</div>

Its not public API but it does work

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 9:12am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/19 "2022-05-17T09:12:48Z")

</div>

[https://playcanvas.com/editor/scene/1419589](https://playcanvas.com/editor/scene/1419589)  
Removing private assets from the original project was too much, so I created a new project to do this.  
Since socket communication is required, it is not clear whether you can connect.  
But the effect is that the mouse wheel can deliver distance data.  
I’m not sure how to modify the distance in orbitCamera

---

<div class="post-metadata">

**Author:** ![leooooooo](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leooooooo/32/26720_2.png) [@leooooooo](https://forum.playcanvas.com/u/leooooooo)\
**Post date:** [May 17, 2022, 9:20am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/20 "2022-05-17T09:20:35Z")

</div>

I just started using playcanvas, there are many things that are not clear

---

<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:** [May 17, 2022, 10:57am UTC](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734/21 "2022-05-17T10:57:10Z")

</div>

I see the issue, this is due to JS scoping and closures rather than PlayCanvas. (More on this here: [You-Dont-Know-JS-1/README.md at master · jumaschion/You-Dont-Know-JS-1 · GitHub](https://github.com/jumaschion/You-Dont-Know-JS-1/blob/master/this%20&%20object%20prototypes/README.md#you-dont-know-js-this--object-prototypes))

```javascript

    socket.onmessage = function (msg) {
        console.log(msg.data);
        var result = JSON.parse(msg.data);
        var msg2 = JSON.parse(result.text);

        if (msg2.entity == "camera") {
            // console.log(msg2.distance);
            this.orbitCamera.distance = msg2.distance;
        } else if (msg2.entity == "camera2") {
            this.orbitCamera.pitch = msg2.pitch;
            this.orbitCamera.yaw = msg2.yaw;
        }

    };

```

You’ve created a function callback here but the scope of the `this` object is the function object, not the script instance object.

To fix this so that the `this` in the function is on the script instance so that you can access the properties, you bind the function to an object of the scope you want which is explained here: [You-Dont-Know-JS-1/ch2.md at master · jumaschion/You-Dont-Know-JS-1 · GitHub](https://github.com/jumaschion/You-Dont-Know-JS-1/blob/master/this%20%26%20object%20prototypes/ch2.md#explicit-binding)

This would make your code:

```javascript
    socket.onmessage = function (msg) {
        console.log(msg.data);
        var result = JSON.parse(msg.data);
        var msg2 = JSON.parse(result.text);

        if (msg2.entity == "camera") {
            // console.log(msg2.distance);
            this.orbitCamera.distance = msg2.distance;
        } else if (msg2.entity == "camera2") {
            this.orbitCamera.pitch = msg2.pitch;
            this.orbitCamera.yaw = msg2.yaw;
        }
    }.bind(this); // Bind the function to the scope of the script instance

```

[Next page](https://forum.playcanvas.com/t/how-do-i-change-orbitcamera-distance-from-another-script/25734.md?page=2)
