What would be the best way(s) to have the player (and/or an object) move onto an animated object (translating and/or rotating ''vehicle'' for instance) and then be ''transported'' by that animated object while keeping its ability to be moved around by mouse/keyboard control until it 'steps off' that vehicle (like stepping on board of a moving ship, and sitting or walking around while being transported, until finally stepping off the ship)? Would anybody have achieved that sort of thing (or tried to..), and would there be any 'scripts' available for that? (or can it be achieved by applying physics, and in that case, how?) Thanks for any help; any idea or piece of advice would be welcome.
asked Dec 26 '09 at 02:26 AM
Basically, all you need to do is put the player-object under the vehicle-object in the hierarchy. That can be done via Transform.parent. So if you have Transform vehicle and Transform player, to get into the vehicle, simply call
Then, when the player leaves the vehicle, simply call
In the case where the player steps onto a car that he would control, you'd also need to transfer control (moving the player around with keyboard / joystick / mouse) from directly influencing the player to influencing the vehicle (and back). So, in that case you'd have some sort of "PlayerController"-class to which you either assign the player-object, or the vehicle-object. You might also change the state if the behavior of the vehicle is different from the behavior of the player (which is usually the case). Obviously, those are very game-specific things.
For the example of the ship in which the player can move around (what you asked for), that would be more like a typical moving platform: In that case, you would keep controlling the player but have it attached to the ship. Then, you just need to make sure that any movement of the player is based on Transform.localPosition instead of Transform.position. This is really where having Transform.localPosition shows its full benefit because using that, these kinds of things get very easy to implement.
In Traces of Illumination, I now do have moving platforms that the player can get onto; which is working quite nicely and wasn't too hard to implement. However, the scripts wouldn't help much because I still need to do a lot of stuff specific to that game; so they would be unnecessarily complex (it's networked multiplayer - so that adds a tremendous amount of complexity). But with the information above and the scripting reference, you should have a very good start. Feel free to ask any more specific questions along the way.
answered Dec 26 '09 at 08:37 AM
I guess parenting your objects Transform will certainly do the trick. But there is another way to do this if you don't want to mess with your hierarchy. You could also use a "Fixed Joint". This will attach your player to the moving object's rigidbody. The advantage is that you don't neet to change hierarchy. However this will only work if both objects have a rigidbody component.
answered Jan 06 '10 at 04:02 PM