- Home /
Riding a motorbike
Scripting is another world to me, and I'm fairly new to Unity. I've made a motorbike, and with the help of someone else from the forums I now have a script that allows me to mount my bike model. I essentially want to walk around as the bike, so when I walk up to it and click on the bike, the bike becomes part of the First Person Controller. I'm not bothered about bike physics for this I literally just want to be able to walk around as the bike. Is there something in the script that stops me doing this? All I can do so far is stand with the bike and look around 360 degrees?
The bike is it's own object in the hierarchy and every object in it has a mesh collider on it.
Here's the script as of now:
var rayDistance : int = 5; //how far will the raycast go?
var cam : Transform; //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.GetMouseButtonDown(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
}
}
}
Thanks
Your answer
Follow this Question
Related Questions
methods of keeping bikes upright ? 1 Answer
Bike tut for bike nut ? 0 Answers
Bike Physics? 1 Answer
Simple Motorbike control HELP! 1 Answer
Adjustable mesh geometry,Create a mesh with adjustable geometry 0 Answers