- Home /
Make an object path across another object
I'm trying to make a cube (cart placeholder) follow along a track:
I'm wondering what the best method is to achieve this. A pathing system like Spline Controller will not suffice because I don't want to have to configure the waypoints on the track; it should just follow it. I was thinking of creating the script like so;
Set the target track object
Position the host object in the middle of the track object
Keep raycasting diagonally downwards from the top-front of the cube to see if there's still a track in front of it
Move forward if so
Optionally have the cart/cube reverse direction if the track ends, in case it doesn't loop
Optionally create the path once at script startup rather than dynamically in case the track is static
Would this work, and would it be o.k. performance wise (in other words; is there a better alternative, performance wise)? Also, what do I need to look in to to make the cart/cube turn across curves so it doesn't end up looking silly?
Answer by robertbu · Jun 26, 2013 at 05:03 PM
The way I see it, to make this work you need two surfaces, one for left and right, and one for up and down. A groove in the track will work, or a simple angle will work...even the edge of the track. Instead of raycasting diagonally downward, I'd place an empty sensor objects in the groove/angle. It would raycast down and to the right. The sensor would adjust itself so that it maintained a set offset to the groove/angle. If you had the object position lag the sensor position, you can use the different positions to define an angle to use as the forward for movement and for the rotation of the cube. You can add a forward raycast to the sensor to sense a stop at the end of the groove/angle. Consider using Collider.Raycast() instead of Physics.Raycast().
This got me thinking, rather than raycasting at all, would it be possible to 'read' the mesh data and simply create waypoints from that? Then I could create an empty object directly above the left track and have that follow the waypoint system, then all I have to do is joint the cube or whatever I want on top of said empty object and it'll path along.
Reading the mesh is problematic unless you either 1) create the mesh in code, or 2) can guarantee some properties of the mesh. I certainly is possible to do the raycasting solution I outline above in Start() to create the path.
But wouldn't that mean I'd have to manually place the sensors?
Vertices don't have to be in any particular order. So it if you were to read the mesh data directly, it could be a difficult, possibly complex, and error prone job to walk the data and generate the path. As for sensors, you'll have to manually place the sensors at the very beginning and also indicate an initial walk direction.
How would this work when I only place the sensors at the beginning? If I understand correctly, you suggest a forward raycast on the sensor to see where the track takes a turn so I can automatically place a sensor there, but that raycast might hit the other side of the track, plus the 'automatic' sensors would need to have a way to alter the track-finding-raycast direction because else it would keep raycasting up even after, say, a right turn, when it should start raycasting to the right. Or am I misunderstanding your suggestion completely?
Answer by killer-zog · Jun 28, 2013 at 01:26 PM
why not use itweenpath system. just modified some line according to what you want and you are good to go.
He doesn't want to use a path. The whole point of the question is how to walk/generate mesh without specifying a path.