How to make a car: Difference between revisions
>Cecibean m Added section for Car Tool |
>Cecibean m Added section for Car Tool |
(No difference)
|
Revision as of 10:24, 13 December 2008
Introduction
This tutorial will demonstrate the basic steps needed to create a car from scratch. It will also describe how to add additional features to the car. The scripts used to make the car move are derived from the basic plane scripts. In that sense, the car has many of the same key objects as the plane. For this tutorial, the plane scripts have been simplified and customized to make the car-specific.
Creating a Car From Scratch
These instructions describe the building of the car within Roblox Studio.
The car will consist of these models:
- CarGarage
- Car
- Parts
- BodyKit
Creating the CarGarage Model
The purpose of CarGarage model is basically to hold the script needed to re-generate the car after a player drives away. Follow these steps to create the CarGarage model and add the necessary objects which will allow the car to regenerate.
- In the Explorer window, select the Workspace
- From the Insert menu, select Object > Model
- Rename this new model CarGarage
- Select the new CarGarage model
- From the Insert menu, select Object > IntValue. Rename this value Regen
- Select the CarGarage model, and insert a Script object. Rename this script RegenScript. Add the following code into the script.
system = script.Parent -- gets the Garage model model = system.Car -- gets the Car model backup = model:Clone() -- creates a copy of the Car model regen = system.Regen -- Saves the integer value stored in the Garage's Regen variable function checkRegen() if regen.Value == 1 then -- When the player starts driving the car, the script -- sets this value to 1 model = backup:Clone() -- copies the copy of the Car model.Parent = system -- adds the plane to the Garage model:MakeJoints() end end regen.Changed:connect(checkRegen) -- When a Car is created, the regen value changes -- from nil to a value - triggering this call.
Creating the Car Model
The Car Model will hold all parts that are needed to make the car. Follow these steps to create the structure of the Car:
- Select the CarGarage Model and insert an object Model. Name this new model Car.
- Select the Car model, and insert an object IntValue. Name this value Stunt.
- Insert into the Car model two additional models - name them Parts and BodyKit.
- The structure of the CarGarage model should now look like this:
Creating the Parts Model
This model will hold all the key pieces that will make the car driveable. Follow these steps to add the necessary pieces:
- Select the Parts model and insert a CFrameValue object. Name this value OriginCFrame.
- Insert into the Parts model a script object. Name this script CarScript.
- Select the following code, copy it and paste it into the CarScript script:
position = script.Parent.Engine.Position -- position of the engine -- Add CFrame to Engine local frame = Instance.new("CFrameValue") frame.Name = "OriginCFrame" frame.Value = script.Parent.Engine.CFrame frame.Parent = script.Parent -- Get the CarGarage local object = Instance.new("ObjectValue") object.Value = script.Parent.Parent.Parent -- Get the Seat seat = script.Parent.Seat function onChildAdded(part) -- if the user sits in the seat if part.className == "Weld" then local torso = part.Part1 if torso ~= nil then local parent = torso.Parent if parent ~= nil then -- assign the CarGarage model to the player script.Parent.Parent.Parent = parent while true do wait(2) local pos = script.Parent.Engine.Position -- once the car is far enough from original spot - -- set the Regen value - so it causes another car to regenerate if (position - pos).magnitude > 30 then if object.Value ~= nil then object.Value.Regen.Value = 1 wait(.5) object.Value.Regen.Value = 0 object.Value = nil end break end end while true do wait(2) if part == nil then script.Parent.Parent.Parent = game.Workspace script.Parent.Parent:MakeJoints() break end end end end end end -- When a player seats in the car seat.ChildAdded:connect(onChildAdded)
- Insert into the Parts model a Seat object. Note, this seat will be the driver's seat.
- Change the Size of the seat to be 2x2 (2 for x axis, and 2 for z axis.)
- Change the formFactor of the seat to be Plate.
- Change the BrickColor property to any color. In this example, the seat is Black.
- Change the FrontSurface property of the Seat to Hinge. This will show which side of the Seat is the front. It is important that the front of the Seat brick is facing the front of the car. The FrontSurface can be changed back to Smooth.
- To make sure the TopSurface is facing up, change it to be type Hinge. Make sure the yellow hinge is pointing up.
- Change the BottomSurface and TopSurface to Weld.
- Insert into the Parts model a Part object. Name it Engine.
- Change the Size of the Engine brick to be x=2, y=1.2, z=16. For this example, this brick will make the bottom of the car.
- Change the BrickColor property to any color. In this example, the Engine brick is Bright red.
- Change the FrontSurface property of the brick to Hinge. This will show which side of the brick is the front. It is important that the front of the Engine brick is facing the front of the car. The FrontSurface can be changed back to Smooth.
- Select the Engine brick, and insert into it the IntValue object. Rename the IntValue object to Count.
- Insert into the Engine brick a Script object. Copy and paste the following code into the Engine script object:
model = script.Parent.Parent.Parent -- get the garage backup = model:clone() -- copy it local debounce = false function onTouch(part) if (part.Name == "Safe") and (debounce == false) and (script.Parent.Count.Value == 0) then debounce = true wait(2) model = backup:clone() model.Parent = game.Workspace model:makeJoints() script.Parent.Count.Value = 1 debounce = false end end -- if Engine brick is touched script.Parent.Touched:connect(onTouch)
- Insert into the Engine brick a BodyGyro object.
- Insert into the Engine brick a BodyPosition object.
- Insert into the Engine brick a BodyVelocity object, and name it Drift.
- Insert into the Parts model an additional 4 Part objects. These bricks will become the wheels of the car. Name the 4 new bricks these names:
LeftWheel1, LeftWheel2, RightWheel1, RightWheel2. For each of these 4 wheel bricks, do the following:
- Make the BackSurface, BottomSurface, FrontSurface Smooth
- Make the size (3,3,3)
- Make the Shape property Cylinder.
- Make the formFactor property Symmetric.
- Set the BrickColor property to Black
- Set the RightSurface property to Motor.
At this point, you should have these pieces in your workspace:
Assembling the Parts
Assemble these key pieces of the car as follows:
- Place the Seat on the front section of the Engine brick.
- Place the wheels on each side of the Engine, where wheels go. Make sure the motor of the wheel is against the Engine brick.
- NOTE: Since it can be difficult to move the wheels while they are inside the model, you may drag the wheels to be under the Workspace object in the Explorer window. This will allow you to move them with the move tool, to make sure they are attached properly to the engine brick. Once that is done, drag the wheels to be back under the Parts model.
Adding the Car Tool
Follow these steps to add the Car Tool to players as they enter the game.
- Insert a HopperBin object under the StarterPack and name it Car Tool
- Select the Car Tool and insert a script object into it. Name the script CarDriver
- Copy the following code into the script.
Creating the BodyKit Model
s