|
|
(43 intermediate revisions by 7 users not shown) |
Line 1: |
Line 1: |
| __TOC__
| | {{Sucks|reason=There's a few broken images on this page. Well okay i'm probably exaggerating when i say few.}} |
|
| |
|
| == Introduction ==
| | Creating Cars using [[RBX.lua.VehicleSeat (Object)|VehicleSeat]] objects is actually really easy. There are certain building methods you need to use, or you can use the cars straight from the [[Insert Menu]]. This tutorial will show you how to use VehicleSeats yourself, so that you can build awesome cars that you design. |
|
| |
|
| This tutorial will demonstrate the basic steps needed to build 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.
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial1.png|400px|thumb |
| | |Creating your own cars is easy. Pull open a new map and put in a baseplate. It should look something like this |
| | ]]</div> |
|
| |
|
| == Creating a Car From Scratch == | | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial2.png|400px|thumb |
| | |Let's grab the parts we need out of the [[Insert Menu]]. You're going to need 4 of the blue wooden wheels, and one of the rectangle blocks. The wooden one looks nice |
| | ]]</div> |
|
| |
|
| These instructions describe the building of the car within Roblox Studio.
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial3.png|400px|thumb |
| | |That wooden block there isn't big enough for us to use, select the blue [[Studio#Brick_Properties|Resize tool]] and make it big enough that it looks like a car. The size in the picture works well, it is 4 studs wide by 11 studs long |
| | ]]</div> |
|
| |
|
| The car will consist of these models:
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial4.png|400px|thumb |
| *CarGarage
| | |Now we can start attaching parts. At the top of the screen select the [[Studio#Surface_Types|Hinge tool]] and left click on the insides of the blue wheels. They should look just like the picture now, with the little yellow peg sticking out of the center. |
| *Car
| | ]]</div> |
| *Parts
| |
| *BodyKit
| |
|
| |
|
| === Creating the CarGarage Model === | | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial5.png|400px|thumb |
| | |Next we attach the wheels to the car. Make sure the wheels are all touching the baseplate, then slide them so that they are up next to the wooden block. You shouldn't be able to see the yellow pegs at all anymore. Make sure that there is still space underneath the block and the ground, it shouldn't be touching. Now you have your tires attached to your car! |
| | ]]</div> |
|
| |
|
| 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.
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial7.png|400px|thumb |
| | |Next we're going to add in the VehicleSeat, which controls the car. Using the [[Object Insert]] menu, which you can open by clicking on '''insert''' at the top of the screen. Then when you see this box open scroll to the right and select the VehicleSeat. Hit OK. |
| | ]]</div> |
|
| |
|
| *In the Explorer window, select the Workspace
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial8.png|400px|thumb |
| *From the Insert menu, select Object > Model
| | |Your VehicleSeat will appear somewhere. You can find it by selecting the [[Studio#Brick_Properties|Move On Axis tool]], the orange arrows. It might even show up underneath your car! Place at the front of your vehicle, such as between the front two wheels. |
| *Rename this new model '''CarGarage'''
| | ]]</div> |
| *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.
| |
|
| |
|
| <pre> | | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial9.png|400px|thumb |
| | |Now we need to make sure that seat faces forward. You can't drive a car sideways! The easiest way to do this is to put a hinge on the front side of the VehicleSeat and then point that forward. Make sure you have the VehicleSeat selected (with the blue box around it) and then look in the [[Properties Window]]. You may have to scroll down to see it, but look for the section that says "Surface", and the look for "Front Surface". It looks like what you see in the picture. Click to the box on the right of "Front Surface", it will give you a drop-down menu list of different things. Select Hinge. You'll see that yellow peg appear on the front of the seat. |
| | ]]</div> |
|
| |
|
| system = script.Parent -- gets the Garage model
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial10.png|400px|thumb |
| model = system.Car -- gets the Car model
| | |Now all you need to do is rotate that VehicleSeat around so that the yellow peg points forward, like a headlight on a car. Once you've done that, your vehicle is all done and ready to be driven around! |
| backup = model:Clone() -- creates a copy of the Car model
| | ]]</div> |
| regen = system.Regen -- Saves the integer value stored in the Garage's Regen variable
| |
|
| |
|
| function checkRegen()
| | <div style="display: inline-block">[[Image:Vehicle_seat_tutorial11.png|400px|thumb |
| if regen.Value == 1 then -- When the player starts driving the car, the script
| | |Hop in and have fun driving your car around! You will see a blue bar in the screen telling you how fast you're going. There are a few property options that you can set, such as how fast you want the maximum speed to be, or how fast the car can turn. You can see these properties [[RBX.lua.VehicleSeat_(Object)|on the VehicleSeat page]]. |
| -- sets this value to 1
| | ]]</div> |
| 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.
| |
| | |
| </pre>
| |
| | |
| Your Explorer window should look something like this:
| |
| | |
| [[Image:CarGarage.PNG]] | |
| | |
| === 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:
| |
| | |
| [[Image:CarModel.PNG]]
| |
| | |
| === 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:
| |
| | |
| <pre>
| |
| 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)
| |
| | |
| | |
| </pre>
| |
| | |
| *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=4, 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:
| |
| | |
| <pre>
| |
| 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)
| |
| </pre>
| |
| ::* 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, TopSurface, and FrontSurface '''Smooth'''
| |
| ::*Make the [[Shape]] property '''Cylinder'''.
| |
| ::*Make the size (3,3,3)
| |
| ::*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:
| |
| | |
| [[Image:Parts.PNG]]
| |
| | |
| === 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.
| |
| *Make sure the Left wheels are on the left side of the car - On your left if you were sitting on the seat facing forward.
| |
| *Make sure the Right wheels are on the right side of the car.
| |
| *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.
| |
| | |
| [[Image:Car1.PNG]]
| |
| | |
| === 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.
| |
| | |
| <pre>
| |
| bin=script.Parent
| |
| car=nil
| |
| hold=false
| |
| local debounce = false
| |
| local cardebounce = false
| |
| local stuntdebounce = false
| |
| local controlling = false
| |
| | |
| function computeDirection(vec)
| |
| local lenSquared = vec.magnitude * vec.magnitude
| |
| local invSqrt = 1 / math.sqrt(lenSquared)
| |
| return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
| |
| end
| |
| | |
| function move(target, engine)
| |
| local origincframe = engine:findFirstChild("BodyGyro").cframe
| |
| local dir = (target - engine.Position).unit
| |
| local spawnPos = engine.Position
| |
| | |
| local pos = spawnPos + (dir * 1)
| |
| | |
| engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(9000, 9000, 9000)
| |
| engine:findFirstChild("BodyGyro").cframe = CFrame.new(pos, pos + dir)
| |
| wait(0.1)
| |
| engine:findFirstChild("BodyGyro").maxTorque = Vector3.new(0, 0, 0)
| |
| engine:findFirstChild("BodyGyro").cframe = origincframe
| |
| end
| |
| | |
| function findCar(player)
| |
| local list = player.Character:GetChildren()
| |
| for x = 1, #list do
| |
| if (list[x].Name == "Car") then
| |
| local weld = list[x]:FindFirstChild("Parts"):FindFirstChild("Seat"):FindFirstChild("SeatWeld")
| |
| if (weld ~= nil) and (weld.Part1 == player.Character:FindFirstChild("Torso")) then
| |
| return list[x]
| |
| end
| |
| end
| |
| end
| |
| return nil
| |
| end
| |
| | |
| function onButton1Down(mouse)
| |
| local vehicle = findCar(script.Parent.Parent.Parent)
| |
| if vehicle ~= nil and debounce == false and cardebounce == false then
| |
| debounce = true
| |
| | |
| controlling = true
| |
| | |
| while true do
| |
| wait()
| |
| local engine = vehicle.Parts.Engine
| |
| local position = mouse.Hit
| |
| local target = position.p
| |
| if engine ~= nil then
| |
| move(target, engine)
| |
| end
| |
| if cardebounce == true or
| |
| controlling == false then break end
| |
| end
| |
| wait(.1)
| |
| debounce = false
| |
| end
| |
| end
| |
| | |
| function onButton1Up(mouse)
| |
| controlling = false
| |
| end
| |
| | |
| function onSelected(mouse)
| |
| mouse.Icon = "rbxasset://textures\\GunCursor.png"
| |
| mouse.Button1Down:connect(function() onButton1Down(mouse) end)
| |
| mouse.Button1Up:connect(function() onButton1Up(mouse) end)
| |
| mouse.KeyDown:connect(onKeyDown)
| |
| end
| |
| | |
| function onKeyDown(key)
| |
| if (key~=nil) then
| |
| key = key:lower()
| |
| local vehicle = findCar(script.Parent.Parent.Parent)
| |
| if (vehicle==nil) then return end
| |
| car = vehicle.Parts
| |
| local engine = vehicle.Parts.Engine
| |
| local r1 = engine.Parent.RightWheel1
| |
| local r2 = engine.Parent.RightWheel2
| |
| local l1 = engine.Parent.LeftWheel1
| |
| local l2 = engine.Parent.LeftWheel2
| |
| if (key=="x") and cardebounce == false then
| |
| r1.RightParamB= 0
| |
| r2.RightParamB= 0
| |
| l1.RightParamB= 0
| |
| l2.RightParamB= 0
| |
| end
| |
| if (key=="y") then
| |
| r1.RightParamB= -1
| |
| r2.RightParamB= -1
| |
| l1.RightParamB= 1
| |
| l2.RightParamB= 1
| |
| end
| |
| end
| |
| end
| |
| | |
| bin.Selected:connect(onSelected)
| |
| | |
| | |
| </pre> | |
| | |
| === Creating the BodyKit Model ===
| |
| s
| |