Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Draw rollercoaster dynamically using bezier

Discussion in 'Scripting' started by luukvanoijen, Nov 2, 2015.

  1. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    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 :D. I tried this to make a bezier curve, but I couldn't let it draw between the RailJoints:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3.    
    4.     class Bezier extends System.Object {
    5.      
    6.      
    7.         var p0 : Vector3;
    8.         var p1 : Vector3;
    9.         var p2 : Vector3;
    10.         var p3 : Vector3;
    11.      
    12.         var ti : float = 0.0;
    13.      
    14.         private var b0 : Vector3 = Vector3.zero;
    15.         private var b1 : Vector3 = Vector3.zero;
    16.         private var b2 : Vector3 = Vector3.zero;
    17.         private var b3 : Vector3 = Vector3.zero;
    18.      
    19.         private var Ax : float;
    20.         private var Ay : float;
    21.         private var Az : float;
    22.      
    23.         private var Bx : float;
    24.         private var By : float;
    25.         private var Bz : float;
    26.    
    27.         private var Cx : float;
    28.         private var Cy : float;
    29.         private var Cz : float;
    30.    
    31.         // Init function v0 = 1st point, v1 = handle of the 1st point , v2 = handle of the 2nd point, v3 = 2nd point
    32.         // handle1 = v0 + v1
    33.         // handle2 = v3 + v2
    34.         function Bezier(v0 : Vector3, v1 : Vector3, v2 : Vector3, v3 : Vector3) {
    35.             this.p0 = v0;
    36.             this.p1 = v1;
    37.             this.p2 = v2;
    38.             this.p3 = v3;
    39.         }
    40.      
    41.         // 0.0 >= t <= 1.0
    42.         function GetPointAtTime(t : float) : Vector3 {
    43.             this.CheckConstant();
    44.             var t2 : float = t * t;
    45.             var t3 : float = t * t * t;
    46.             var x : float = this.Ax * t3 + this.Bx * t2 + this.Cx * t + p0.x;
    47.             var y : float = this.Ay * t3 + this.By * t2 + this.Cy * t + p0.y;
    48.             var z : float = this.Az * t3 + this.Bz * t2 + this.Cz * t + p0.z;
    49.             return(Vector3(x,y,z));
    50.          
    51.         }
    52.      
    53.         private function SetConstant() {
    54.             this.Cx = 3 * ((this.p0.x + this.p1.x) - this.p0.x);
    55.             this.Bx = 3 * ((this.p3.x + this.p2.x) - (this.p0.x + this.p1.x)) - this.Cx;
    56.             this.Ax = this.p3.x - this.p0.x - this.Cx - this.Bx;
    57.          
    58.             this.Cy = 3 * ((this.p0.y + this.p1.y) - this.p0.y);
    59.             this.By = 3 * ((this.p3.y + this.p2.y) - (this.p0.y + this.p1.y)) - this.Cy;
    60.             this.Ay = this.p3.y - this.p0.y - this.Cy - this.By;
    61.    
    62.             this.Cz = 3 * ((this.p0.z + this.p1.z) - this.p0.z);
    63.             this.Bz = 3 * ((this.p3.z + this.p2.z) - (this.p0.z + this.p1.z)) - this.Cz;
    64.             this.Az = this.p3.z - this.p0.z - this.Cz - this.Bz;
    65.    
    66.         }
    67.    
    68.         // Check if p0, p1, p2 or p3 have changed
    69.         private function CheckConstant() {
    70.             if (this.p0 != this.b0 || this.p1 != this.b1 || this.p2 != this.b2 || this.p3 != this.b3) {
    71.                 this.SetConstant();
    72.                 this.b0 = this.p0;
    73.                 this.b1 = this.p1;
    74.                 this.b2 = this.p2;
    75.                 this.b3 = this.p3;
    76.             }
    77.         }
    78.     }
    79.    
    80.    
    81.     var myBezier : Bezier;
    82.     private var t : float = 0.0;
    83.    
    84.     function Start() {
    85.         myBezier = new Bezier(Vector3(-5,0,0), Random.insideUnitSphere * 2.0, Random.insideUnitSphere * 2.0, Vector3(5,0,0));
    86.     }
    87.    
    88.     function Update () {
    89.         var vec : Vector3 = myBezier.GetPointAtTime(t);
    90.         transform.position = vec;
    91.      
    92.         t+= 0.001;
    93.         if (t > 1.0)
    94.             t = 0.0;
    95.      
    96.     }
    97.    
    98.  
    Could anyone help me draw my rollercoaster? I would like it if it worked with unlimited RailJoints and if it loop around :D.

    --Lucky
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I made this to get you started.
     

    Attached Files:

  3. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    Thx, I will take a look at it, see if I can work with it. Thx again :D
     
  4. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    If you would like, I could send you the some updates on the game?
     
  5. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    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 :D.

    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.
     
  6. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    Oke, so now I use your scene, but I am going to modify the crap out of it :D. 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 :D
     
  7. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    Oke, so how do I add my own mesh in? I tried this:
    Code (CSharp):
    1. mesh = (Mesh)Resources.Load("Assets/Rails/Models/RollercoasterRail",typeof(Mesh));
    2.         mesh.name = "rail_mesh";
    3.         mesh.MarkDynamic();
    4.         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?
     
  8. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    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.
     
  9. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    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 :D
     
  10. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    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.
     
  11. luukvanoijen

    luukvanoijen

    Joined:
    Sep 15, 2015
    Posts:
    32
    Nvm, lol, I forgot that rmb rotated stuff :D
     
  12. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    There is also the steps variable on the manager. The rest is up to the bezier knots and tangents.