Create the track manager script and customize the inspector by the class derived by editor which using the unityEditor namespace .
consider some problems in the track manager
first we need the race line node to storedeach node information
it has a variable is t value that's represented as the percentage of left wall node position to right wall node position.And then we also need the position to mark the current node position ,And the max speed to trigger this node to slow down the car speed to current maximum speed of this interface
so this is the single race line node above and then we need the race line interface to store some race line nodes:
we need the middle position of the interface that's represented as the pivot point in that interface
we also need the quaternion to cache thecurrent rotation by this interface in order to generate the right vector that ‘srepresented as the positive right vector to that interface,And we can alsogenerate the left wall node poisiton and the right wall node position by thisright vector
so we visualize this is transform and needsthree normal vector to represented the transform matrix to transform thecurrent interface object .
the right vector is basics vector in thefirst row of the transform matrix
the up vector is basics vector in thesecond row of the transform matrix
the look vector is basics vector in thethird row of the transform matrix
also we need the two vector to cache theleft wall node positin and the right wall node position
And also we need the width of thisinterface to calculate the final left wall node positin and the final rightwall node position
And we also need the radius that ‘srepresented as the influence range of this interface at which AI car shouldslow down it’s speed to correspond to that target speed of this interface
And we also need the track distance to knowhow long it is between this interface and the last interface
And we also need the list to store the how many race line nodes in this interface ,the list count must be at most 5 count,
And then we also need to expose the public accessible properties to the other class which want to call them
In the constructor of the this interface weneed to pass third parameters to that method
The first parameter is the position of thethis interface which will be called by the OnSceneGUI method in the class whichderived by the editor ,And the second parameter is quaternion of this interfacewill be also called by the OnSceneGUI method in some class which derived by theeditor
And third parameter is the LayerMask valuethat would be used to detect the track mesh object ,And let the interface knowthe corresponding position Y,which would be placed in the track
Inside the constructor the last commandline is return the three normal vector including right up look vector by thequaternion rotation which will be call the function in the math utility toolwhich
Contains the basics math operation aboutconvert the quaternion to the 4 by 4 transform matrix ,
That the matrix will tell us how the three normalvector looks like.
In the method we just assign the currentvalue to the passed value ,And allocate the new memory of the both left wallnode and the right wall node value
And we need the SetPosition method to placethe point of the interface on the track mesh
And first thing we had to do is to modifythe y component of the position we just passed it in the first parameter of themethod , And change it value to the 99999 height ,And we also need ray to castthe negative global up vector to scene ,and the ray will tell us thecorresponding y component of the position it has ,so we just only change the yvalue of the position to the hit.point.y code
And the last thing we had to do is calculatethe both position of the left wall node and the right wall node
So the CalculateWallNode method justcalculate the corresponding position of the both wall node
By the right vector we just declared in theabove property and the half of the width of the track we also need take intoaccount.
The next method is theRecalculateRaceLineNodes which is recalculate by the each race line node property:tValue ,and we declare the delta value that’s cache the vector between rightwall node and the left wall node position And loop through the race line nodeslist and calculate the position of this node by scaling the delta vector withtValue in this race line node
Next mothd is RecalcualteRaceLineNode whichpassed the index parameter and second tvalue parameter ,in the beginning of themethod we need check whether this index we just passed is illegal value of thatlist or not ,if this is the illegal value of this list we just return andterminate this method ,otherwise we calculate the current index of the raceline node position by the tValue
Next method is SetTrackDistance whichpassed the only float value as it distance,And pass it to the currenttrackDistance property Next we also want to know the track distance in thatinterface so we need the another relative method is GetTrackDistance which justreturn the trackDistance just declared above
And next we want to know how to add therace line node the race line node list of this interface ,So we can visualizethat method should be called AddNewRaceNode which passed the previous interfacereference which will expose the corresponding race line node position to thatmethod and calculate the final position by that property.In the side method wejust declare the temporary value cache the newNode by type of RaceLineNode ,And make sure the wall nodeposition is up to date So we call the CalcualteWallNode function And the nextstate we should check the count of the race line nodes list ,it must be at most5 elements in this list ,so if it’s greater than and equal to 5 and this functionwill be terminated ,if the count is less than 5 and then we should allocate thememory to the newNode temporary value ,and add the reference to race line nodeslist ,And make the count plus plus .and next thing we had to do is check if thecurrent count is greater than the previous list count ,just assign the tvalueand maxspeed it has in the highest index of the previous list else we just put the property of count -1 index of the previous list
To the maxspeed and the tvalue .last weshould calculate the current position of that new node by scaling the tValue
The next we do is passed the index to themethod and return the corresponding race node element of the list if the indexis greater than the highest index ,we just return the reference of the highestindex of the interface This is the GetMatchingRaceNode()
And we want the getRaceLineNode by passedthe index to it ,and return the reference of current index of the race linenodes list ,is the index we passed is illegal we return the null reference toit
When we Add race line node in the list, wealso need the DeleteRaceLineNode(int index) function to delete the given indexelement of the race line nodes list
At the beginning of this function we justcheck the given index is the illegal index ,if it is ,terminated the function,otherwise just call the RemoveAt function to remove the given index element ofthe race line list
When we modify the width value in thecustomize inspector we want to call the SetWidth function which just passed thewidth value to the width of the interface above
SetRotation function which passed thequaternion value and will change the current quaternion and regenerate thethree vector ,and recalculate the wall nodes
And we also need the SetRadius function tomodify the influence range to this interface
All of it will be called by the class whichderived by the editor class
About the track manager it must be asingleton class that other class will call it reference just allocate once ,Andthe track manager has the property of current interface index and the interfacespacing just move along the look vector by scale this value to some distancebetween the last interface And need some Boolean for the show interface settingand show interface creation setting show interface editor setting and show raceline node Editor setting
Then we need some color for represented asthe wall node and the race line node
And the cast layer we also has to cache toray cast layer mask
And the track length we also need to be calculate the distance the car traveled.
in the track manager we need to create the new interface and delete the current interface .
the cast layer mask just is the byte operation the total cast layer mask is the interger which is 256 8 bit ,you can use the one bit of them to set the value false or true ,we can visualize if we want to show some handles(Gizmos) in the sence view and use the first bit of it (_castLayer&1) and _castLayer&1<<1 and etc you can use it to do something that 8 bool value will do
The next section we will discuss the class which is derived by the editor class,in this class we can add the new button and some fancy GUI to the trackManager script ,the OnInspectorGUI() will override the some content will show in the inspector when you hit the gameobject which the trackmanager script attached .