Draw rollercoaster dynamically using bezier

Hi,
I’m trying to make a game where people make a rollercoaster. I made it to the point where I instantiate a block called RailJoint where I click, it has the tag “st”. Now I simply want to draw a bezier line between those RailJoints and draw a mesh over it. It isn’t going to be a very complicated mesh, because I am not good at that :smile:. I tried this to make a bezier curve, but I couldn’t let it draw between the RailJoints:

#pragma strict

    
    class Bezier extends System.Object {
      
      
        var p0 : Vector3;
        var p1 : Vector3;
        var p2 : Vector3;
        var p3 : Vector3;
      
        var ti : float = 0.0;
      
        private var b0 : Vector3 = Vector3.zero;
        private var b1 : Vector3 = Vector3.zero;
        private var b2 : Vector3 = Vector3.zero;
        private var b3 : Vector3 = Vector3.zero;
      
        private var Ax : float;
        private var Ay : float;
        private var Az : float;
      
        private var Bx : float;
        private var By : float;
        private var Bz : float;
    
        private var Cx : float;
        private var Cy : float;
        private var Cz : float;
    
        // Init function v0 = 1st point, v1 = handle of the 1st point , v2 = handle of the 2nd point, v3 = 2nd point
        // handle1 = v0 + v1
        // handle2 = v3 + v2
        function Bezier(v0 : Vector3, v1 : Vector3, v2 : Vector3, v3 : Vector3) {
            this.p0 = v0;
            this.p1 = v1;
            this.p2 = v2;
            this.p3 = v3;
        }
      
        // 0.0 >= t <= 1.0
        function GetPointAtTime(t : float) : Vector3 {
            this.CheckConstant();
            var t2 : float = t * t;
            var t3 : float = t * t * t;
            var x : float = this.Ax * t3 + this.Bx * t2 + this.Cx * t + p0.x;
            var y : float = this.Ay * t3 + this.By * t2 + this.Cy * t + p0.y;
            var z : float = this.Az * t3 + this.Bz * t2 + this.Cz * t + p0.z;
            return(Vector3(x,y,z));
          
        }
      
        private function SetConstant() {
            this.Cx = 3 * ((this.p0.x + this.p1.x) - this.p0.x);
            this.Bx = 3 * ((this.p3.x + this.p2.x) - (this.p0.x + this.p1.x)) - this.Cx;
            this.Ax = this.p3.x - this.p0.x - this.Cx - this.Bx;
          
            this.Cy = 3 * ((this.p0.y + this.p1.y) - this.p0.y);
            this.By = 3 * ((this.p3.y + this.p2.y) - (this.p0.y + this.p1.y)) - this.Cy;
            this.Ay = this.p3.y - this.p0.y - this.Cy - this.By;
    
            this.Cz = 3 * ((this.p0.z + this.p1.z) - this.p0.z);
            this.Bz = 3 * ((this.p3.z + this.p2.z) - (this.p0.z + this.p1.z)) - this.Cz;
            this.Az = this.p3.z - this.p0.z - this.Cz - this.Bz;
    
        }
    
        // Check if p0, p1, p2 or p3 have changed
        private function CheckConstant() {
            if (this.p0 != this.b0 || this.p1 != this.b1 || this.p2 != this.b2 || this.p3 != this.b3) {
                this.SetConstant();
                this.b0 = this.p0;
                this.b1 = this.p1;
                this.b2 = this.p2;
                this.b3 = this.p3;
            }
        }
    }
    
    
    var myBezier : Bezier;
    private var t : float = 0.0;
    
    function Start() {
        myBezier = new Bezier(Vector3(-5,0,0), Random.insideUnitSphere * 2.0, Random.insideUnitSphere * 2.0, Vector3(5,0,0));
    }
    
    function Update () {
        var vec : Vector3 = myBezier.GetPointAtTime(t);
        transform.position = vec;
      
        t+= 0.001;
        if (t > 1.0)
            t = 0.0;
      
    }

Could anyone help me draw my rollercoaster? I would like it if it worked with unlimited RailJoints and if it loop around :smile:.

–Lucky

I made this to get you started.

2366031–160651–Rails.unitypackage (40.5 KB)

Thx, I will take a look at it, see if I can work with it. Thx again :smile:

If you would like, I could send you the some updates on the game?

ThermalFusion, I have a script that instantiates your RailJoint prefab, but if I instantiate one, it says that the RailJoint has something in the script that isn’t assigned, see error message at bottom. If I look at the script, in that spot it says this, but Unity doesn’t take that :smile:.

ERROR:
NullReferenceException: Object reference not set to an instance of an object
RailJoint.Start () (at Assets/Rails/Scripts/RailJoint.cs:12)

EDIT:
It does recognize this, it doesn’t know what RailPlacer is.

Oke, so now I use your scene, but I am going to modify the crap out of it :smile:. What I am going to do is make a 3d rail, make it use a modifyable terrain and make a cart ride it. Thx for your help :smile:

Oke, so how do I add my own mesh in? I tried this:

mesh = (Mesh)Resources.Load("Assets/Rails/Models/RollercoasterRail",typeof(Mesh));
        mesh.name = "rail_mesh";
        mesh.MarkDynamic();
        GetComponent<MeshFilter>().sharedMesh = mesh;

but this doesn’t load in anything?!? I checked if the mesh name inside RollercoasterRail blender file was actually rail_mesh, but do I need to make it an fbx?

What you are asking to do is rather complex. I dont mean to be harsh but you seem to be in way over your head. You are probably better off looking for a finished spline deformation system like MegaFiers. The solution I provided does not deform an existing mesh, it creates a new one procedurally, but you did not pick up on that. I should have explained it is a basic example that I provided. It does some of what you ask, but there is going to be a lot of extra effort needed to put in there.

Oke, so I am just reading a bit on procedurally generated meshes, so I might try to draw my own one then. But I know, this is probably way over my head, as I’m 15, but I’ll try, because if I don’t do anything, I won’t learn anything new. Again, a big thank you, sir. You are the best :smile:

Oke, so I haven’t started on the mesh part, but do you know how I can make the track more smooth? I already tried different settings, but it just won’t work for me.

Nvm, lol, I forgot that rmb rotated stuff :smile:

There is also the steps variable on the manager. The rest is up to the bezier knots and tangents.