That’s an interesting solution, not quite what I had in mind, but good food for thought.
Here’s the afore mentioned code I’ve been working on. Now I have to figure out how I want to attach and detach the character - which I’m still thinking over.
My first goal is thus: When the player collides with the box, attach the player to the rail at the position where the player collided with the box. Then I’ll force the player controls to move only towards the next point and still allow the player controls to move forward and backward on the rail, then I’ll add the constant motion and physics/momentum stuff.
Again, I want it to be versatile, so with only a little modification you can apply it to whatever situation.
For anyone interested, here is the rail generation code at the moment. If you can think of a more efficient way to handle any of this, feel free to make recommendations.
// The next RailPoint, this variable needs to be assigned in the inspector.
var nextRailpoint : RailPoint;
var lastRailpoint : RailPoint;
var endRailpoint : boolean = false;
private var rail : Object;
// Set the rail's collider width and height, the length is determined by distance between this RailPoint and the next
var railWidth = 1;
var railHeight = 1;
//Point to the CollisionBox prefab
var boxPrefab : Transform;
function Awake () {
//Generate the CollisionBoxes on Awake - if it's not the end of the rail.
if (nextRailpoint) {
GenerateCollisionBox(nextRailpoint.transform.position);
}
}
function Update () {
// If it's not the end of the rail
if (nextRailpoint != null) {
// For testing purposes only, updates the rail if you drag it around in the Scene.
UpdateCollisionBox(nextRailpoint.transform.position, rail);
}
}
// Dynamically creates and scales the collision box for the rail from a prefab cube
function GenerateCollisionBox (position : Vector3) {
// Create the rail collision box...
rail = Instantiate ( boxPrefab, transform.position, transform.rotation);
// Call the update to set the size and position...
UpdateCollisionBox(position, rail);
}
// Use this in your Update() function if you have have a dynamically moving/stretching waypoint...
function UpdateCollisionBox (position : Vector3, rail) {
// Determine the length of the box by comparing the distance between the two points
var railLength = Vector3.Distance (transform.position, position);
// ... and point the box at the next target. Then set the size and position it.
// Finally make it a child of the current RailPoint.
rail.transform.LookAt(nextRailpoint.transform);
rail.collider.size = Vector3(railHeight,railWidth,railLength);
rail.collider.center.z = railLength/2;
rail.transform.parent = transform;
}
// Draw the RailPoint pickable gizmo
function OnDrawGizmos () {
Gizmos.DrawIcon (transform.position, "R.tif");
}
// Draw the RailPoint lines only when you select one of the waypoints
function OnDrawGizmosSelected () {
if (nextRailpoint) {
Gizmos.color = Color.green;
Gizmos.DrawLine (transform.position, nextRailpoint.transform.position);
}
if (lastRailpoint) {
Gizmos.color = Color.red;
Gizmos.DrawLine (transform.position, lastRailpoint.transform.position);
}
}
EDIT: Okay. A bit more practical of a question. So I’m not sure how to distribute the code. The code above is great for making the rails. But now I need to figure out how to distribute the functionality. I need the player to collide with the above boxes. Then attach to the rail at the point. Get the next or current rail point (based on current angle of the player on collision). Move the player onto the rail and make it look at the next point and only allow players to move forward toward that point until collision and then get the next point and so on.
So I need to need to detect when the player hits the box and then move the player based on that collisions. So it sounds like I need to have a new script for the boxes detecting the collisions that passes info to the player maybe? Then the RailRide script on the player to control the players movement? And some how pass info around these three scripts? Hmm… That’s not sounding very clear to me at this point on how to divide up the functionality.