- Home /
Simple Motorbike control HELP!
I'm pretty new to Unity. I've made a motorbike in 3DS Max, with all of the objects grouped into "frame", "front wheel" and "back wheel" (see the links below). What I want to be able to do is have the first person controller be able to mount the bike and then move it as they would if they were still in the first person controller mode. I don't need any wheel animation, no turning of the handle bars, just the ability to mount the bike and essentially walk around as the bike. Is there any simple way to do this? Thanks
Answer by AutoFredrik · Apr 06, 2013 at 06:50 PM
Just attatch the bike model as a child to the first person controller and it will follow the player wherever he goes! You do this by placing the bike in a position where it looks like the player is riding it and, in the hierachy drag the bike to the controller to make it a child of the controller. That's a very simple solution, but it will do what you asked for. If you want the player to be able to walk around and click on the bike to get up on it, you will need to do some scripting. It would basically be a script where you check if the player clicks on the bike using raycast, and then move the bike to where the player is (or the other way around) and parent the bike to the controller. I can help you with that too, if you want to. Also, make sure that the bike has no collider attached or things will get messed up when the player moves. Hope this helps. Nice model by the way!
Oh! That sounds so simple, you're so right! :P I'll give that a try, thanks so much for your quick response. I would appreciate some help with the scripting if it's quite short - don't want to take too much of your time. And thanks, took a good while.
Ok, so I wrote a new answer for you about the scripting part, but when I hit "Submit" it said I couldn't post 2 answers on the same question, and everything I wrote was lost. Aaaargh! :) Anyway, I wrote this script for you, add it to your charater.
`var rayDistance : int = 5; //how far will the raycast go?
var cam : Tranform; //assign this to your camera.
var yOffset : float = 0.5; //how high above the bike should the character be when he sits on it?
function Update()
{
var hit : RaycastHit; //stores info about what the ray hits
if(Physics.Raycast(cam.position, cam.forward, hit, rayDistance) && Input.Get$$anonymous$$ouseButtonDown(0))
/*looks for objects in front of us and checks if we click*/
{
if(hit.transform.root.name == "Bike") //if we did click we will check if the ray hit the bike.
{
var bike : Transform = hit.transform.root; //stores the bike in a variable
for(var bikePart in bike)
bikePart.collider.enabled = false;
transform.position = Vector3(bike.position.x, bike.position.y + yOffset, bike.position.z); /*moves character. a little bit above the bike. The higher the yOffset the higher above the bike he sits.*/
bike.rotation.y = transform.rotation.y; //rotates the bike so we're not riding sideways.
bike.parent = transform; //parents the character to the bike so it will follow us
}
}
}`
Just make sure that the bike or the bike's children have colliders. I hope this works! EDIT code now turns of the colliders of the bike's children when mounted.
Just simply Edit your answer to include the code. Write :
Hope this helps. Nice model by the way!
Edit :
Here is some script to demonstrate :
// add your code
// to your answer
Oh damn, I'm sorry it did that. That's awesome thanks so much for writing this! So, now I should have the player and bike separated in the hierarchy, save this code as a script in Unity and just drop it onto my character?
Yepp, pretty much. Just make sure to assign the "cam" variable to the camera attached to your first person controller. And remove the "`" in the end and beginning of the script, I wrote it from my phone and they showed up for some reason. The script can't make the player get off the bike, he can only get on :) And don't forget to add som form of collider to the bike.