Cookbook (Chapter 7)

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

One of the key concepts in programming in ROBLOX is positioning. Chapter 2 was dedicated entirely to positioning and positioning systems. Now we're going to take it a step further and talk about moving. ROBLOX provides a ridiculous amount of so called “Body” objects that control movement and rotation.

One way to simulate movement is to interpolate between two positions, however this gets messy when trying to do rotations and more complex movement. That's the advantage of using these body objects.

Later in this chapter we will also look at connection objects like Welds and Motors. These are used to do attachment and rotation.

Moving to a Position

Problem

You want to get a part to move to another position.

Solution

Use the BodyPosition object.

local b = Instance.new('BodyPosition')
b.maxForce = Vector3.new(math.huge, math.huge, math.huge)
b.position = Vector3.new(10, 10, 10)
b.Parent = Workspace.Part

Discussion

We create the BodyPosition object and set its maxForce property to a Vector3 with each axis set to math.huge. What we want to do is make sure there is no limit to how much force can be applied to the part. Then we change the position to another Vector3. Then we parent the BodyPosition to the part we want to move.

Moving in a Direction

Problem

You want to get a part to move in a direction.

Solution

Use the BodyVelocity object.

local b = Instance.new('BodyVelocity')
b.maxForce = Vector3.new(math.huge, math.huge, math.huge)
b.velocity = Vector3.new(0, 10, 0)
b.Parent = Workspace.Part

Discussion

The BodyVelocity object uses its Velocity property to determine speed and direction.

Rotating in a Direction

Problem

You want to get a part to rotate in a direction.

Solution

Use the BodyAngularVelocity object.

local b = Instance.new('BodyAngularVelocity')
b.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
b.angularvelocity = Vector3.new(0, 10, 0)
b.Parent = Workspace.Part

Discussion

The BodyAngularVelocity object uses its angularelocity property to determine speed and rotation. A good diagram of how this works can be found by clicking here. This time instead of using the maxForce property, we used maxTorque which is rotational power.

Using BodyGyro

Problem

You want to get a part to face a Vector3 constantly.

Solution

Use the BodyGyro object.

local b = Instance.new('BodyGyro')
b.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
b.cframe = CFrame.new(0, 10, 0)
b.Parent = Workspace.Part

Discussion

The BodyGyro object creates a gyroscope affect where the part its inside of always points towards a fixed point. This point is defined by a CFrame in the BodyGyro's cframe property.

Using Dampening

Problem

You want to control the dampening of an object.

Solution

Use the D property.

local b = Instance.new('BodyPosition')
b.maxForce = Vector3.new(math.huge, math.huge, math.huge)
b.position = Vector3.new(10, 10, 10)
b.D = 20
b.Parent = Workspace.Part

Discussion

The default dampening is 1250 which results in pretty much no dampening curve. I particularly like the setting 20 because it gives the motion an elastic like easing.

Using Power

Problem

You want to change the amount of power in the motion.

Solution

Use the P property.

local b = Instance.new('BodyPosition')
b.maxForce = Vector3.new(math.huge, math.huge, math.huge)
b.position = Vector3.new(10, 10, 10)
b.P = 100
b.Parent = Workspace.Part

Discussion

The default power is 10000 which is quite quick. By setting the power to 100, the motion takes a considerably longer amount of time to reach its goal.

Using BodyForce

Problem

You want to move a part using a certain amount of force.

Solution

Use the BodyForce object controlled with the force property.

local b = Instance.new('BodyForce')
b.force = Vector3.new(0, 5000, 0)
b.Parent = Workspace.Part

Discussion

What's the difference between BodyForce and BodyVelocity? With BodyVelocity you set the velocity in which the part moves in. No matter how large the part is, it will move at that constant velocity. With BodyForce, you set the force in which the part is being moved. Therefore the bigger the object, the more force it will require to move.

Using BodyThrust

Problem

You want to move a part in a certain direction with a certain force.

Solution

Use the BodyThrust object controlled with the force property.

local b = Instance.new('BodyThrust')
b.force = Vector3.new(0, 5000, 5000)
b.location = Vector3.new(0, 100, 100)
b.Parent = Workspace.Part

Discussion

The BodyThrust object uses its force property to control the force on each respective axis. Then it uses its location property to determine the direction in which is going to apply the force.

Simple Welding

Problem

You want to weld two parts together.

Solution

Use Weld object.

local w = Instance.new('Weld', Workspace)
w.Part0 = Workspace.Player.Head
w.Part1 = Instance.new('Part', Workspace)

Discussion

The Weld object attaches two objects together. In this case I'm taking a newly created part, and attaching it to a Player's head. The Part0 property is the part that is being attached two, and Part1 is the attached part. The Weld, must be in the Workspace, however it is not required that it be in either of the parts to take affect.

Weld Offset

Problem

You want to offset a weld's attachment.

Solution

Use C0 and C1 properties.

local w = Instance.new('Weld', Workspace)
w.Part0 = Workspace.Player.Head
w.Part1 = Instance.new('Part', Workspace)
w.C0 = CFrame.new(10, 0, 0)

Discussion

This will basically offset the weld's attachment point to 10 studs to the right. The part will move with the Player, just 10 studs to the right of the head. You may also use the C1 property instead of the C0 property, and the attachment point will be 10 studs to the right of the head.

Simple Motors

Problem

You want to manipulate a motor.

Solution

Use the DesiredAngle property.

Workspace.Player.Animate.Disabled = true
Workspace.Player.Torso['Right Shoulder'].DesiredAngle = -math.pi
wait(1)
Workspace.Player.Animate.Disabled = false

Discussion

There are several motors in the Torso part that control the rotation and connect the limbs to the torso. The Animate script inside of the character controls the character's limb rotation. In order to manipulate it without the Animate script interfering, we must disable it first. The DesiredAngle property is set the the desired angle and the motor automatically rotates the part accordingly.

Motor Speed

Problem

You want to manipulate the speed in which a motor rotates.

Solution

Use the MaxVelocity property.

Workspace.Player.Animate.Disabled = true
Workspace.Player.Torso['Right Shoulder'].DesiredAngle = -math.pi
Workspace.Player.Torso['Right Shoulder'].MaxVelocity = 1
wait(1)
Workspace.Player.Animate.Disabled = false

Discussion

The MaxVelocity property controls the maximum velocity allowed for the rotation of a motor to take place. Its set at .1 by default.

Joints

A joint is a bond or connection between two objects through what are called snaps. To create a joint you use the MakeJoints method, or the BreakJoints method in order to remove the connections. In order to create a join, two bricks must have the required surface types in order to form it.

By using a Glue or Weld surface, it creates the respective type of connection between the two. Also note that when loading a ROBLOX you'll notice a brick count and a connector count. The connector count is the amount of snaps that are in game.

Continue on to Chapter 8, Miscellaneous