Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you rotate an object without Vector 3?

I decided to make this spotlight thing I have going on rotate. At first I tried using stuff like for loops to make it smooth but then I decided I would start by making it just move for now. But when I tried it, it didn't work I looked around and somewhere it said that Vector3 doesn't rotate objects or something like that so I don't know how I'm suppose to do this. Any help would be great.

SpotLight = game.Workspace.SpotLight.Union
local Button = script.Parent
function onClicked(mouse)
	SpotLight.Rotation = SpotLight.Rotation + Vector3.new (0,5,0)
end

Button.MouseButton1Click:connect(onClicked)

1 answer

CFrame library

Objects should be positioned, and rotated with CFrame. CFrame is a library built for working with position and rotation of an object in a 3D space, and has multiple different constructors for working with it.

CFrame.new

CFrame.new will return a CFrame object that you can change the properties, and arguments of to give it different mechanics. This, however, isn't going to be a full CFrame tutorial, so that you can mess with on your own time. For now, all you need to know is that CFrame.new is how you can create a new CFrame value.

It pretty much works the same way as Vector3 (in terms of position), with these exceptions:

  • CFrame does not avoid obstructions when positioning an object (unlike using Position and Vector3), therefore it has the ability to position objects anywhere, regardless of collision.

  • CFrame can take multiple different orders of arguments, or argument data types that will alter how something is positioned / rotated.

Here's an example:

local part = Instance.new("Part", workspace)

-- Each BasePart actually has a hidden property (not shown in the studio properties window) called 'CFrame'. This is the property you must index in order to set a new CFrame to the part.
part.CFrame = CFrame.new(0, 5, 0) -- Positions the part at 0:X, 5:Y, 0:Z

As you can see, you could give it a set of coordinates just like Position and Vector3, and it will work the same way (obviously, with considering the differences between the two stated in the list of exceptions above).

CFrame.Angles

Despite mentioning how rotation can be altered by passing extra arguments to CFrame.new, most people prefer using the CFrame.Angles constructor to work with rotation (in most generic cases), because of it's simplicity to work with and understand. You should note, however, CFrame.Angles cannot be used to set the CFrame property of an object. Only CFrame.new may do this. This is because other constructors besides CFrame.new are actually meant to alter a currently existing CFrame.

This alteration is done by multiplying CFrame objects together, to add / subtract position and rotation (much like adding or subtracting one Vector3 value from another). Here's an example:

local part = Instance.new("Part", workspace)

-- This is how you'd change a CFrame's rotation by multiplying it with CFrame.Angles, however, we don't have any arguments for our rotation yet...
part.CFrame = CFrame.new(0, 5, 0) * CFrame.Angles()

But what arguments does CFrame.Angles take? Well, numbers, obviously. But it's important to know what kind.

Rotation and Radians

A radian is the unit of an angle. Take for example, pi, which is meant to be half of a 360 degree rotation represented by the numbers 3.1415... which goes on infinitely, or just shortened to 3.14. This is because pi is represented in radians.

Lua's math library provides some pretty handy functions for dealing with degrees and radians, and even gives us the value of pi. This can be very useful for just about anything geometric, or dealing with circles and rotation.

So let's say for example, you wanted to rotate an object 180 degrees on it's X axis. You could simply just pass the value of pi to it's respective position in CFrame.Angles, like so:

local part = Instance.new("Part", workspace)

part.CFrame = CFrame.new(0, 5, 0) * CFrame.Angles(math.pi, 0, 0) -- Rotate it 180 degrees on it's X axis

That's the gist of it. Alternatively, you can set the CFrame of one object, to exactly that of another just by assigning it to the CFrame property directly. This assignment will copy everything about the operand CFrame (including rotation), making it practical to use in situations where you want to copy the exact position and rotation of an object, in a very short compact way. Here's an example:

local part1 = Instance.new("Part", workspace)
local part2 = Instance.new("Part", workspace)

-- If you only want to effect the rotation of an object and leave position alone, you could just set the objects CFrame to itself, while giving it a new rotation matrix, like so:
part1.CFrame = part1.CFrame * CFrame.Angles(math.pi, 0, 0)

-- This will set part2's CFrame to the exact same position as part1's, with the same 180 degree angle on it's X axis.
part2.CFrame = part1.CFrame

That's about all I'm going to cover in this answer, but if you'd like to know more, just let me know and I'll respond with an updated answer and some informative links. Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.