- Home /
how could a player (or an object) be moved onto and inside an animated vehicle?
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.
Answer by jashan · Dec 26, 2009 at 08:37 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
player.parent = vehicle;
Then, when the player leaves the vehicle, simply call
player.parent = null;
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.
many thanks, I'll study your explanations for the moment and might be interested in 'networked multiplayer' info in a not so remote future.
Answer by StephanK · Jan 06, 2010 at 04:02 PM
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.
Your answer
Follow this Question
Related Questions
How to add physics to an animation? 3 Answers
Collision occurring when it should not! Help! 1 Answer
What is the best practice in handling scenes with 100+ rigidbodies and collisions on iOS? 1 Answer
Is there a way to use physics to keep a character from clipping through itself during an animation? 0 Answers
Help with 2D Hit Detection! 0 Answers