Search Unity

Curved Horizon Shader - Control Script Help Required

Discussion in 'Scripting' started by Deleted User, May 25, 2017.

  1. Deleted User

    Deleted User

    Guest

    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 :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class curveController : MonoBehaviour
    6. {
    7.  
    8.     public Transform CurveOrigin;
    9.  
    10.     [Range(-500f, 500f)]
    11.     [SerializeField]
    12.     float x = 0f;
    13.  
    14.     [Range(-500f, 500f)]
    15.     [SerializeField]
    16.     float y = 0f;
    17.  
    18.     [Range(0f, 50f)]
    19.     [SerializeField]
    20.     float falloff = 0f;
    21.  
    22.     private Vector2 bendAmount = Vector2.zero;
    23.  
    24.     // Global shader property ids
    25.     private int bendAmountId;
    26.     private int bendOriginId;
    27.     private int bendFalloffId;
    28.  
    29.     void Start ()
    30.     {
    31.         bendAmountId = Shader.PropertyToID("_BendAmount");
    32.         bendOriginId = Shader.PropertyToID("_BendOrigin");
    33.         bendFalloffId = Shader.PropertyToID("_BendFalloff");
    34.     }
    35.  
    36.     void Update ()
    37.     {
    38.         bendAmount.x=x;
    39.         bendAmount.y=y;
    40.  
    41.         Shader.SetGlobalVector(bendAmountId, bendAmount);
    42.         Shader.SetGlobalVector(bendOriginId, CurveOrigin.position);
    43.         Shader.SetGlobalFloat(bendFalloffId, falloff);
    44.     }
    45. }
     
  2. Deleted User

    Deleted User

    Guest

    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class curveController : MonoBehaviour
    6. {
    7.     private float currentCurvatureX;
    8.     private float newCurvatureX;
    9.     private float currentCurvatureY;
    10.     private float newCurvatureY;
    11.  
    12.     // Time Interval - time to wait
    13.     [Header("Time Interval - Seconds")]
    14.     public float timeInterval = 30f;
    15.  
    16.     // Curve Origin - Drag Main Camera into this public slot
    17.     [Header("Curve Origin - Main Camera")]
    18.     public Transform CurveOrigin;
    19.  
    20.     // Curvature X
    21.     [Range(-500f, 500f)]
    22.     [SerializeField]
    23.     [Header("Curvature X")]
    24.     float x = 0f;
    25.  
    26.     // Curvature Y
    27.     [Range(-500f, 500f)]
    28.     [SerializeField]
    29.     [Header("Curvature Y")]
    30.     float y = 0f;
    31.  
    32.     // Falloff Effect
    33.     [Range(0f, 50f)]
    34.     [SerializeField]
    35.     [Header("Falloff Effect")]
    36.     float falloff = 0f;
    37.  
    38.     private Vector2 bendAmount = Vector2.zero;
    39.  
    40.     // Global shader property ids
    41.     private int bendAmountId;
    42.     private int bendOriginId;
    43.     private int bendFalloffId;
    44.  
    45.     void Start ()
    46.     {
    47.         bendAmountId = Shader.PropertyToID("_BendAmount");
    48.         bendOriginId = Shader.PropertyToID("_BendOrigin");
    49.         bendFalloffId = Shader.PropertyToID("_BendFalloff");
    50.  
    51.         // Starting after timeInterval ( wait x amount of seconds ), call
    52.         // changePathCurvature function every timeInterval ( repeat every x amount of seconds )
    53.         InvokeRepeating("changePathCurvature", timeInterval, timeInterval);
    54.     }
    55.  
    56.     void Update ()
    57.     {
    58.         bendAmount.x=x;
    59.         bendAmount.y=y;
    60.  
    61.         Shader.SetGlobalVector(bendAmountId, bendAmount);
    62.         Shader.SetGlobalVector(bendOriginId, CurveOrigin.position);
    63.         Shader.SetGlobalFloat(bendFalloffId, falloff);
    64.     }
    65.  
    66.     void changePathCurvature()
    67.     {
    68.         // x - Get current value of CurvatureX
    69.         currentCurvatureX = x;
    70.  
    71.         // newCurvatureX = Random within a range of -50 and 50
    72.         newCurvatureX = (Random.Range(-50.0f, 50.0f));
    73.  
    74.         // x = Lerp currentCurvatureX to newCurvatureX over timeInterval
    75.         x = Mathf.Lerp(currentCurvatureX, newCurvatureX, timeInterval);
    76.  
    77.         // y - Get current value of CurvatureY
    78.         currentCurvatureY = y;
    79.  
    80.         // newCurvatureY = Random within a range of -50 and 50
    81.         newCurvatureY = (Random.Range(-50.0f, 50.0f));
    82.  
    83.         // y = Lerp currentCurvatureY to newCurvatureY over timeInterval
    84.         y = Mathf.Lerp(currentCurvatureY, newCurvatureY, timeInterval);
    85.     }
    86. }
     
    Smilzo_ likes this.