0

I am making a Train Simulator using Unity3D 5 and I want to bend the train wagon smoothly at a curved track and back to normal at a straight track, how do I do it?

I am using Hermite Spline Controller C# Version,

Here is code.

using UnityEngine;
using System.Collections;

public class BendTrain : MonoBehaviour {

    public Transform trainwagon2;
    public Transform trainwagon3;
    public Transform waypoint2;
    public Transform waypoint3;
    public static bool t2 = false;
    public static bool t3 = false;

    void OnTriggerEnter (Collider col1)
    {

                if (col1.tag == "b2") {
                    t2=true;
                }


                if (col1.tag == "b3") {
                    t3=true;
                }
    }

    void Update ()
    {
        if (t2)
        trainwagon2.transform.rotation = Quaternion.RotateTowards(trainwagon2.transform.rotation, waypoint2.rotation, 2);

        if (t3)
        trainwagon3.transform.rotation = Quaternion.RotateTowards(trainwagon2.transform.rotation, waypoint2.rotation, 2);

    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
Jamshaid Alam
  • 515
  • 1
  • 9
  • 24

1 Answers1

0

This is pretty tricky, I haven't done this by myself but here is a basic idea of how to do this:

  1. Calculate distance by using start point and end point of curved path.
  2. Use curve shader to bend the train object accordingly.
  3. Need to write a script to apply this shader when object is at curve path.and remove it later.

Here is a sample (not tested) code to start with:

Shader "Custom/Curved" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _QOffset ("Offset", Vector) = (0,0,0,0) _Brightness ("Brightness", Float) = 0.0 _Dist ("Distance", Float) = 100.0 }

 SubShader {
     Tags { "Queue" = "Transparent"}
     Pass
     {

         Blend SrcAlpha OneMinusSrcAlpha 
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"


         sampler2D _MainTex;
         float4 _QOffset;
         float _Dist;
         float _Brightness;

         struct v2f {
             float4 pos : SV_POSITION;
             float4 uv : TEXCOORD0;
             float3 viewDir : TEXCOORD1;
             fixed4 color : COLOR;
         };
         v2f vert (appdata_full v)
         {
            v2f o;
            float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
            float zOff = vPos.z/_Dist;
            vPos += _QOffset*zOff*zOff;
            o.pos = mul (UNITY_MATRIX_P, vPos);
            o.uv = v.texcoord;
            return o;
         }
         half4 frag (v2f i) : COLOR0
         {
               half4 col = tex2D(_MainTex, i.uv.xy);
               col *= UNITY_LIGHTMODEL_AMBIENT*_Brightness;
             return col;
         }
         ENDCG
     }
 }

 FallBack "Diffuse"

References:

https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/

http://forum.unity3d.com/threads/problem-with-bending-terrain-shader.229605/

https://alastaira.wordpress.com/2013/10/25/animal-crossing-curved-world-shader/

http://answers.unity3d.com/questions/288835/how-to-make-plane-look-curved.html

I hope this gives you a hint.

Umair M
  • 10,298
  • 6
  • 42
  • 74