Centering an object on another's collider

Is there a good way to center a moving object on the collider of another object - like a train running along a track, for instance? I imagine it would use “OnTriggerStay” but I don’t know the kind of code that would calculate the center of the moving object and keep it on track right…

I have balls going down tracks here
http://www.ryuuguu.com/unity/webv032.html
(click on the tiles on the screen or the black dots)
For each track section I have a collider at each end. when a mover hits a track collider it store a refernce to the track it is now on and asks the track where it destination is. The track tells the move where it should be going and the mover than use slerp (for the curved movement.) to change its position to get there. For a straight piece of track use lerp. you either have to break yuor track in to straigh pireceis and curves or use spline to calculate where your mover should be.

Cheers,
Grant

you do indeed. great stuff.

Thanks for your response, ryuuguu. That IS cool. But it’s …er …above my head right now (hopefully someday it won’t be).
The motion I am using is much simpler so I am hoping for a simpler solution. My player is only moving in straight lines along the z axis. So I only need to shift the player along the x axis so that it is centered on the “rails” collider. Is there someway to just measure the two colliders and shift the player’s x axis location accordingly?

I though Lerp might not be so scary after all and played with this code (in a OnTriggerStay function):

transform.position = Vector3.Lerp (
        transform.position, other.transform.position,
        Time.deltaTime * 2);

but I wasn’t able to adjust it so that the player was only being moved in one direction. " transform.position.x, other.transform.position.x" wasn’t accepted by Unity. Can I make this so that it works the way I want??[/code]

Is this what you want?

If you want it to “jump” on to the rail you could put this in the player script

function OnTriggerStay (railCollider : Collider) {
    transform.location.x = railCollider.transform.location.x;
}

so until the player moves off the collider by changing Z they will keep othe x value of the collider.

If this is not what you want please let me know.
There are not many Unity coders in Japan, yet :slight_smile:
Cheers,
Grant

Just saw your new message. here is what you want I think

ty=transform.position.y;
tz=transdform.position.z;
ox=other.transform.position.x
transform.position = Vector3.Lerp ( 
        transform.position, Vector3(ox,ty,tz), 
        Time.deltaTime * 2);

Grant, both of those did the trick but the second one gave smooth motion so it won the prize. Thanks!
I am actually from America, but my home for the past decade has been Nagano, Japan (and may well stay that way for many more decades). I see you’re in Tokyo?