Hi I'm making a first person http://shooter.So far characters can walk around, shoot ai's and pick up stuff but I'd like to let them get in vehicles.I don't have a script for a car or anything so I'd need that http://too.So overall I need a script to let a character get in ,control, then get out of a car.
asked Apr 25 '10 at 09:30 PM
Ok, this is just theoric, but it could be something as simple as this.
Create a singleton with a property or a global variable with the name "Controller" where controller is a gameobject
Create a car object (something very simple would do) that can be driven by the player if that gameobject is the same than Controller
Create a character object that can also be controlled by the player if the gameobjeect is the same as controller
Create a camera script that points to "Controller" all the time.
Presto! now all you have to do is a global object or a coroutine that checks a key to switch the controller variable from one gameobject to the another. Neat!
Ok now that you've done that make sure that the key switches from player to car only if they are at a certain distance. (you could even check if you are near the door)
Now for the final trick
Ok one way to do this easily is to simply dissapear the character y not rendering it whenever he is not the controller but lets try something more elegant.
Create a new transform child object in the car and place it where the seat of the player would be lets name this "seat" (it could also be a seat model)
Change the script so it makes the seat transform into the parent object of our character when the car is the controller and place the character at position 0,0,0 (if your character is animated you could make it sit)
Theres a problem though, when you "exit the car" the character would still be in the same position 0,0,0, which means it would still be inside the car, to fix this move it to your left or right vector by a few units (if the character is still sitting make it stand). That way your character will exit the car whenever he is out. (this could also be used in case you decided to just dissapear the character so it moves around with the vehicle)
Thats it, is a pretty simple but functional way to move characters around in vehicles. Try it!
answered May 05 '10 at 11:11 PM
to make the character get inside I suggest you use the position of your seat object and move your character there, probably localPosition will be better for this. Once there, change the animation to a "sitting animation" on loop, crossfade it with the character moving his arms a bit(don't loop this one) when you press left and right for a better driving experience. To exit, you can either a: create 3 Random.Ranges with let's say 1,3 and subtract it from the player transform.position to make it exit a few inches from the driver seat, then change the animation back to standing.
answered Oct 24 '10 at 12:48 AM
Here is the script, read the script to know what do with the variables: