Hi,
I’m using the shader and control script from the following blog posting :
http://blog.onebyonedesign.com/games/unity3d-endless-runner-part-i-curved-worlds/
At the moment, I can only control the curvature in x or y and once set and the game is run, that is the only direction that will persist. In the interests of making things a bit more visually attractive, I’d like to edit the script to include some randomised curvature over a specific / or random amount of time.
So, for instance, I could have
TIME - every 30 seconds or so ( or within a range ) change the X and Y curvature
X and Y curvature - random between, let’s say, X (-50 and 50 ), and Y ( -50 and 50 )
This way I could generate an endless road with inclines and declines and left and right turns every 30 seconds or so, I’m not sure how to best tackle this, any pointers or help would be greatly appreciated ! 
I’ve posted the curveController.cs script below to showcase what is currently happening :
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class curveController : MonoBehaviour
{
public Transform CurveOrigin;
[Range(-500f, 500f)]
[SerializeField]
float x = 0f;
[Range(-500f, 500f)]
[SerializeField]
float y = 0f;
[Range(0f, 50f)]
[SerializeField]
float falloff = 0f;
private Vector2 bendAmount = Vector2.zero;
// Global shader property ids
private int bendAmountId;
private int bendOriginId;
private int bendFalloffId;
void Start ()
{
bendAmountId = Shader.PropertyToID("_BendAmount");
bendOriginId = Shader.PropertyToID("_BendOrigin");
bendFalloffId = Shader.PropertyToID("_BendFalloff");
}
void Update ()
{
bendAmount.x=x;
bendAmount.y=y;
Shader.SetGlobalVector(bendAmountId, bendAmount);
Shader.SetGlobalVector(bendOriginId, CurveOrigin.position);
Shader.SetGlobalFloat(bendFalloffId, falloff);
}
}
Hi,
I’ve made a basic attempt at this, and have gotten decent results so far. At the moment, my code will bend the curvature randomly within a range successfully every 30 seconds or so ( based on any time I set ), the only problem I have now, is my Mathf.Lerp function does not seems to work as intended, there is no smoothing, curvature changes every 30 seconds are abrupt and do not interpolate as required, I have posted the code I have created so far in the hope someone could offer some help with this ? Please see the changePathCurvature function :
Thanks in advance
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class curveController : MonoBehaviour
{
private float currentCurvatureX;
private float newCurvatureX;
private float currentCurvatureY;
private float newCurvatureY;
// Time Interval - time to wait
[Header("Time Interval - Seconds")]
public float timeInterval = 30f;
// Curve Origin - Drag Main Camera into this public slot
[Header("Curve Origin - Main Camera")]
public Transform CurveOrigin;
// Curvature X
[Range(-500f, 500f)]
[SerializeField]
[Header("Curvature X")]
float x = 0f;
// Curvature Y
[Range(-500f, 500f)]
[SerializeField]
[Header("Curvature Y")]
float y = 0f;
// Falloff Effect
[Range(0f, 50f)]
[SerializeField]
[Header("Falloff Effect")]
float falloff = 0f;
private Vector2 bendAmount = Vector2.zero;
// Global shader property ids
private int bendAmountId;
private int bendOriginId;
private int bendFalloffId;
void Start ()
{
bendAmountId = Shader.PropertyToID("_BendAmount");
bendOriginId = Shader.PropertyToID("_BendOrigin");
bendFalloffId = Shader.PropertyToID("_BendFalloff");
// Starting after timeInterval ( wait x amount of seconds ), call
// changePathCurvature function every timeInterval ( repeat every x amount of seconds )
InvokeRepeating("changePathCurvature", timeInterval, timeInterval);
}
void Update ()
{
bendAmount.x=x;
bendAmount.y=y;
Shader.SetGlobalVector(bendAmountId, bendAmount);
Shader.SetGlobalVector(bendOriginId, CurveOrigin.position);
Shader.SetGlobalFloat(bendFalloffId, falloff);
}
void changePathCurvature()
{
// x - Get current value of CurvatureX
currentCurvatureX = x;
// newCurvatureX = Random within a range of -50 and 50
newCurvatureX = (Random.Range(-50.0f, 50.0f));
// x = Lerp currentCurvatureX to newCurvatureX over timeInterval
x = Mathf.Lerp(currentCurvatureX, newCurvatureX, timeInterval);
// y - Get current value of CurvatureY
currentCurvatureY = y;
// newCurvatureY = Random within a range of -50 and 50
newCurvatureY = (Random.Range(-50.0f, 50.0f));
// y = Lerp currentCurvatureY to newCurvatureY over timeInterval
y = Mathf.Lerp(currentCurvatureY, newCurvatureY, timeInterval);
}
}
1 Like