Scale objects based on distance and the treadmill effect


I am going crazy trying to either google for, or solve on my own, a couple of particular issues (likely easy for advanced users):

1.) How do i make the CHARACTER “move the environment” as opposed to moving itself? I know i’ve seen references to a “treadmill effect” but have NO clue how to author such a thing.

2.) Is it possible to scale objects larger or smaller based on distance from, say, the Main Camera? If so, how? If so I would immensely appreciate a basic example (any language if scripting is involved).

Ultimately, combining these two elements, what I am going for is the ability to place a sphere (a makeshift planet, lets say) relatively close to the camera. As I approach this object, it should ‘appear’ to get closer, but in reality is only scaling up. Additionally, the player itself is not moving, rather the environment itself is moving. From what I’ve read, this implies that I can “treadmill” virtually unlimited content, and ‘movement-wise’, I wont need to worry about floating-point inaccuracies (as much).

I have experimented with ‘function OnTriggerEnter’ whereas gameObjects were instantiated and deleted as needed. To be clear, this DID work … sort of … however not very smoothly and from what I’ve read, this in fact may not be what I need.

I believe, if my two above questions are answered, I will have acquired precisely what I need to move forward.

And for those of you who know perfectly well that there are a few other threads on this same topic, I found them very intellectually argumentative and lacking a final ‘resolution’ (or at least one I could comprehend). In short, if they had a viable answer, I wouldn’t be posting these queries right now, so please forgive me ahead of time. =)

Thank you

Just have to say that moving the player as opposed to moving the entire environment is a MUCH better option and saves on power. But if you want to do it anyway, you can always store the environment under a parent object (call it “world” or something) and move that object instead of the player when you get input.
Essentially resulting in a treadmill.

Unity has a very nice in-built function under Vector3 called Distance, accessed as Vector3.Distance(position1, position2) and returns the distance as a float.

To scale objects depending on distance, you can do something like the following (This script needs to be placed on EVERY object, but theres other ways to do it of course, this is just an example)
This is in c#, and remember this is untested code. However theres only one thing you really need to change if it’s wrong.

public class scaleToCameraDistance : MonoBehaviour {

    private float startingDistance;
    private Vector3 startingScale;

	void Start()
        //Get starting distance to scale objects by, this is the control.
        startingDistance = Vector3.Distance(Camera.mainCamera.transform.position, transform.position);
        //Get starting scale of the object, in the previous version it would have scaled everything to one.
        startingScale = transform.localScale;

    void Update()
        //Figure out the current distance by finding the difference from starting distance
        float curDistance = Vector3.Distance(Camera.mainCamera.transform.position, transform.position) - startingDistance;
        // or was it the other way around, this code is untested!

        //Scale this object depending on distance away to the starting distance
        transform.localScale = startingScale * curDistance;

Excellent - thank you! I do not mind having to tweak it slightly.

Just have to say that moving the player as opposed to moving the entire environment is a MUCH better option and saves on power.

Just for the sake of learning, if you were in my shoes, how would you “do it your way”? I must be reading the wrong posts, because I thought my concept was right when it came to space travel (e.g: where moving the environment becomes a good idea).

Again, much appreciated.

I presume you are referring to moving the origin to prevent the loss of floating point precision. I wrote a script to do that:

based on: