static void RenderGizmo(MegaTrainFollow mod, GizmoType gizmoType)
    {
        if ((gizmoType & GizmoType.Active) != 0 && Selection.activeObject == mod.gameObject)
        {
            if (!mod.showrays)
            {
                return;
            }

            for (int i = 0; i < mod.carriages.Count; i++)
            {
                MegaCarriage car = mod.carriages[i];

                Handles.color = Color.white;
                Handles.DrawLine(car.b1, car.b2);
                //Gizmos.DrawSphere(car.b1, car.length * 0.025f);
                //Gizmos.DrawSphere(car.b2, car.length * 0.025f);
                Handles.SphereCap(0, car.cp, Quaternion.identity, car.length * 0.025f);
                Handles.SphereCap(0, car.b1, Quaternion.identity, car.length * 0.025f);
                Handles.SphereCap(0, car.b2, Quaternion.identity, car.length * 0.025f);
                //if ( showrays )
                Handles.color = Color.red;
                Handles.DrawLine(car.cp, car.bp1);
                Handles.SphereCap(0, car.bp1, Quaternion.identity, car.length * 0.025f);

                //Gizmos.color = Color.green;
                Handles.color = Color.green;
                //Gizmos.DrawLine(car.cp, car.bp2);
                Handles.DrawLine(car.cp, car.bp2);

                //Gizmos.DrawSphere(car.bp2, car.length * 0.025f);
                Handles.SphereCap(0, car.bp2, Quaternion.identity, car.length * 0.025f);
            }
        }
    }
    public override void OnInspectorGUI()
    {
        MegaTrainFollow mod = (MegaTrainFollow)target;

#if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019
        EditorGUIUtility.LookLikeControls();
#endif

        mod.path = (MegaShape)EditorGUILayout.ObjectField("Path", mod.path, typeof(MegaShape), true);

        if (mod.path && mod.path.splines != null)
        {
            if (mod.path.splines.Count > 1)
            {
                mod.curve = EditorGUILayout.IntSlider("Curve", mod.curve, 0, mod.path.splines.Count - 1);
            }

            if (mod.curve < 0)
            {
                mod.curve = 0;
            }
            if (mod.curve > mod.path.splines.Count - 1)
            {
                mod.curve = mod.path.splines.Count - 1;
            }
        }

        mod.distance = EditorGUILayout.FloatField("Distance", mod.distance);
        mod.speed    = EditorGUILayout.FloatField("Speed", mod.speed);
        mod.showrays = EditorGUILayout.Toggle("Show Rays", mod.showrays);

        if (mod.carriages.Count < 1)
        {
            if (GUILayout.Button("Add"))
            {
                MegaCarriage car = new MegaCarriage();
                mod.carriages.Add(car);
            }
        }

        for (int i = 0; i < mod.carriages.Count; i++)
        {
            MegaCarriage car = mod.carriages[i];

            EditorGUILayout.BeginVertical("Box");

            car.length   = EditorGUILayout.FloatField("Length", car.length);
            car.bogeyoff = EditorGUILayout.FloatField("Bogey Off", car.bogeyoff);

            car.carriage       = (GameObject)EditorGUILayout.ObjectField("Carriage", car.carriage, typeof(GameObject), true);
            car.carriageOffset = EditorGUILayout.Vector3Field("Carriage Off", car.carriageOffset);
            car.rot            = EditorGUILayout.Vector3Field("Carriage Rot", car.rot);
            car.bogey1         = (GameObject)EditorGUILayout.ObjectField("Front Bogey", car.bogey1, typeof(GameObject), true);
            car.bogey1Offset   = EditorGUILayout.Vector3Field("Front Bogey Off", car.bogey1Offset);
            car.bogey1Rot      = EditorGUILayout.Vector3Field("Front Bogey Rot", car.bogey1Rot);
            car.bogey2         = (GameObject)EditorGUILayout.ObjectField("Rear Bogey", car.bogey2, typeof(GameObject), true);
            car.bogey2Offset   = EditorGUILayout.Vector3Field("Rear Bogey Off", car.bogey2Offset);
            car.bogey2Rot      = EditorGUILayout.Vector3Field("Rear Bogey Rot", car.bogey2Rot);

            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button("Add"))
            {
                MegaCarriage nc = new MegaCarriage();
                mod.carriages.Add(nc);
            }

            if (GUILayout.Button("Delete"))
            {
                mod.carriages.Remove(car);
            }

            EditorGUILayout.EndHorizontal();
        }

        if (GUI.changed)                //rebuild )
        {
            EditorUtility.SetDirty(target);
        }
    }
Exemple #3
0
    void Update()
    {
        distance += speed * Time.deltaTime;
        if (path)
        {
            float cdist = distance;

            MegaSpline spline = path.splines[curve];

            // Start at front of train and work backwards, first obj in list is head of train
            for (int i = 0; i < carriages.Count; i++)
            {
                float alpha = cdist / spline.length;

                MegaCarriage car = carriages[i];

                float tw  = 0.0f;
                float tw1 = 0.0f;

                car.b1 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha, true, ref tw));

                float d      = cdist - car.length;              //bogey2Offset.z;
                float alpha1 = d / spline.length;

                car.b2 = path.transform.TransformPoint(path.InterpCurve3D(curve, alpha1, true, ref tw1));

                car.cp = (car.b1 + car.b2) * 0.5f;

                //if ( showrays )
                //	Debug.DrawLine(b1, b2);

                // Carriage is positioned half way between the bogeys
                if (car.carriage)
                {
                    // Offset can adjust this, so lerp from b1 to b2 based on offset
                    //Vector3 cp = (b1 + b2) * 0.5f;

                    //float twist = (tw + tw1) * 0.5f;
                    //Quaternion trot = Quaternion.Euler(0.0f, twist, 0.0f);
                    car.carriage.transform.position = car.cp + car.carriageOffset;
                    Quaternion erot = Quaternion.Euler(car.rot);

                    Quaternion rot = Quaternion.LookRotation(car.b1 - car.b2);
                    car.carriage.transform.rotation = rot * erot;                       // * trot;
                }

                if (car.bogey1 && car.carriage)
                {
                    car.bogey1.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey1Offset);                          //b1;

                    Quaternion erot = Quaternion.Euler(car.bogey1Rot);

                    float a = alpha - (car.bogeyoff / spline.length);
                    car.bp1 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true));
                    Vector3    p2  = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true));
                    Quaternion rot = Quaternion.LookRotation(p2 - car.bp1);
                    car.bogey1.transform.rotation = rot * erot;

                    //if ( showrays )
                    //	Debug.DrawLine(car.cp, car.bp1, Color.red);
                }

                if (car.bogey2 && car.carriage)
                {
                    car.bogey2.transform.position = car.carriage.transform.localToWorldMatrix.MultiplyPoint(car.bogey2Offset);                          //b1;

                    Quaternion erot = Quaternion.Euler(car.bogey2Rot);

                    //float tw = 0.0f;
                    float a = alpha1 + (car.bogeyoff / spline.length);
                    car.bp2 = path.transform.TransformPoint(path.InterpCurve3D(curve, a, true, ref tw));
                    Vector3    p2  = path.transform.TransformPoint(path.InterpCurve3D(curve, a + 0.0001f, true));
                    Quaternion rot = Quaternion.LookRotation(p2 - car.bp2);
                    //Quaternion trot = Quaternion.Euler(-tw, 0.0f, 0.0f);
                    car.bogey2.transform.rotation = rot * erot;                    // * trot;

                    //if ( showrays )
                    //	Debug.DrawLine(cp, p1, Color.blue);
                }

                // Then move back a bit for next car
                cdist -= car.length;
            }
        }
    }
	public override void OnInspectorGUI()
	{
		MegaTrainFollow mod = (MegaTrainFollow)target;

		EditorGUIUtility.LookLikeControls();

		mod.path = (MegaShape)EditorGUILayout.ObjectField("Path", mod.path, typeof(MegaShape), true);

		if ( mod.path && mod.path.splines != null )
		{
			if ( mod.path.splines.Count > 1 )
				mod.curve = EditorGUILayout.IntSlider("Curve", mod.curve, 0, mod.path.splines.Count - 1);

			if ( mod.curve < 0 )	mod.curve = 0;
			if ( mod.curve > mod.path.splines.Count - 1 )
				mod.curve = mod.path.splines.Count - 1;
		}

		mod.distance = EditorGUILayout.FloatField("Distance", mod.distance);
		mod.speed = EditorGUILayout.FloatField("Speed", mod.speed);
		mod.showrays = EditorGUILayout.Toggle("Show Rays", mod.showrays);

		if ( mod.carriages.Count < 1 )
		{
			if ( GUILayout.Button("Add") )
			{
				MegaCarriage car = new MegaCarriage();
				mod.carriages.Add(car);
			}
		}

		for ( int i = 0; i < mod.carriages.Count; i++ )
		{
			MegaCarriage car = mod.carriages[i];

			EditorGUILayout.BeginVertical("Box");

			car.length = EditorGUILayout.FloatField("Length", car.length);
			car.bogeyoff = EditorGUILayout.FloatField("Bogey Off", car.bogeyoff);

			car.carriage = (GameObject)EditorGUILayout.ObjectField("Carriage", car.carriage, typeof(GameObject), true);
			car.carriageOffset = EditorGUILayout.Vector3Field("Carriage Off", car.carriageOffset);
			car.rot = EditorGUILayout.Vector3Field("Carriage Rot", car.rot);
			car.bogey1 = (GameObject)EditorGUILayout.ObjectField("Front Bogey", car.bogey1, typeof(GameObject), true);
			car.bogey1Offset = EditorGUILayout.Vector3Field("Front Bogey Off", car.bogey1Offset);
			car.bogey1Rot = EditorGUILayout.Vector3Field("Front Bogey Rot", car.bogey1Rot);
			car.bogey2 = (GameObject)EditorGUILayout.ObjectField("Rear Bogey", car.bogey2, typeof(GameObject), true);
			car.bogey2Offset = EditorGUILayout.Vector3Field("Rear Bogey Off", car.bogey2Offset);
			car.bogey2Rot = EditorGUILayout.Vector3Field("Rear Bogey Rot", car.bogey2Rot);

			EditorGUILayout.EndVertical();

			EditorGUILayout.BeginHorizontal();

			if ( GUILayout.Button("Add") )
			{
				MegaCarriage nc = new MegaCarriage();
				mod.carriages.Add(nc);
			}

			if ( GUILayout.Button("Delete") )
				mod.carriages.Remove(car);

			EditorGUILayout.EndHorizontal();
		}

		if ( GUI.changed )	//rebuild )
		{
			EditorUtility.SetDirty(target);
		}
	}