如何在Unity3D 5中使用Hermite样条控制器弯曲火车车厢?

时间:2016-09-23 04:33:03

标签: c# unity3d game-engine spline hermite

我正在使用Unity3D 5制作一个火车模拟器,我想在弯曲的轨道上平稳地弯曲火车车厢并在直线轨道上恢复正常,我该怎么做?

我正在使用Hermite Spline Controller C#版本,

这是代码。

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);

    }
}

1 个答案:

答案 0 :(得分:0)

这非常棘手,我自己也没有这样做,但这里有一个基本的想法:

  1. 使用弯曲路径的起点和终点计算距离。
  2. 使用curve shader相应地弯曲火车对象。
  3. 当对象处于曲线路径时,需要编写脚本以应用此着色器。稍后将其删除。
  4. 以下是一个示例(未测试)代码:

    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"
    

    参考文献:

    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

    我希望这会给你一个提示。