0

我正在使用 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);

    }
}
4

1 回答 1

0

这很棘手,我自己没有这样做,但这里是如何做到这一点的基本想法:

  1. 使用曲线路径的起点和终点计算距离。
  2. 使用曲线着色器相应地弯曲火车对象。
  3. 当对象位于曲线路径时,需要编写脚本来应用此着色器。稍后将其删除。

这是一个示例(未经测试)代码开始:

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

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

于 2016-09-23T12:38:58.540 回答