How can I use curves to bend Horizons smoothly?

265 Views Asked by At

I have been developing an endless runner game for some time and I wanted it try and make the environment to bend horizon randomly like subway surfaces I download this Horizon Bending asset from the store which works beautifully. In the asset I have a method called ApplyCurvature(float val) which applies it into the scene and bend the desired amount (right now it curves only left ApplyCurvature(5f);).

I want it to be randomly change the curvature such as left, middle or right. I did try out Random methods just to make sure that I can apply curvature for each frame in the game, but it isn't what I want. I thought about using curves which is given by unity scripts but I don't know how to apply them. Can anyone help me on this? I'm new to unity3d so how can I use curves to apply the effect through the asset I have downloaded. Help would be appreciated.

using Battlehub.HorizonBending; //the asset package import

private float hb;

void Update () 
    {

        //HB.ApplyCurvature(5);
        if (playerTransform.position.z - safeZone > (spawnZ - amtOfTilesOnScreen * tileLength))
        {
            if (activeTiles.Count < (NumberOfTilesThatFitOnScreen + 1))
            {
                var spawnedTile = SpawnTileAtFront();

                activeTiles.Add(spawnedTile);
            }
            else
            {
                var movedTile = activeTiles[0];
                MoveTileToTheFront(movedTile);
                activeTiles.RemoveAt(0);
                activeTiles.Add(movedTile);
                //HB.ApplyCurvature(5);

                hb = Random.Range(-5, 5); //Randomly bend road left and right
                HB.ApplyCurvature(hb);
            }
        }
    }

This above code is what I tried to randomly update the bend amount to left and right.

1

There are 1 best solutions below

2
On

Is this what you're trying to do? Visual representation of the desired effect

In that case I think you can't use the ApplyCurvature function for the desired effect. My guess is that it is making a quadratic equation and changing the k value to bend the graph. Example: https://www.desmos.com/calculator/djv3r9enl3 (Use the k slider to see what happens)

I know barely anything about how the camera effects in unity works but I do know some things about graphs. If you can figure out how to change the appearance of the horizon from a function these should be handy:

Left: k * x^2 + sP

Where k = (tP - sP) / t^2

Middle: k * x^2

Where k is your desired value

Right: k * (t - x)^2 + tP

Where k = (sP - tP) / t^2

k: "curvature"

t: target x (To the right of the screen)

sP: starting position y (The lowest point)

tP: target position y (The highest point)

I hope this can be to any help.

EDIT:

To gradually decrease the value you could make a timer and use the Time.deltaTime.

Like so:

float curveTimer = 0;
float curve;

void Start() {
}

void Update () {
    if (curveTimer < yourTargetTime)
        curveTimer += Time.deltaTime;
    else
        curveTimer = 0;

    curve = 5 - (curveTimer / yourTargetTime) * 10;
}

Then input the curve to your function.