For what I am making I need to rotate a part like you can in Unity where you use transform.localRotation. Does Roblox have anything like this or a way to replicate it? I think how transform.localRotation works is it acts as a offset rotation so I am pretty sure it is like a secondary rotation modifier on top of the primary one from what I’ve seen and that is what I need but I am not sure how.
Based on my research (unfortunately am not too familiar with Unity’s code), Transform.localRotation
refers to the rotation of an object relevant to the rotation of its parent. The easiest way to rotate a part based on the rotation of its parent would be through using CFrame
. Here is an example of how you would do this:
-- part is the target part to rotate
local parent = part.Parent
local localRotation = CFrame.Angles(x,y,z) -- local rotation in radians, use math.rad() to convert from degrees
local parentRotation = parent.CFrame - parent.Position -- removing the position element from the Parent's CFrame, leaving us with the rotation
part.CFrame = CFrame.new(part.Position)*parentRotation*localRotation -- set the part's CFrame to its position, and rotated based on the parent and local rotation
Hope this helps!
You are probably right I think I was looking at it wrong as I am only manipulating one part and not 2 so maybe I am in search of something else.
Ah okay. Are you trying to rotate a part based off of its current rotation? Or just trying to rotate one part in general?
I have a gun recoil script and there is a rotation Vector3 I wish to offset the camera’s rotation without interfering with it. Simply doing * CFrame.Angles()
won’t work due to it being an offset type of value and not one I want to add on top of the current rotation each frame. If any of that makes sense.
Some things I’ve tried are
camera.CFrame = camera.CFrame:ToObjectSpace(CFrame.new(camera.CFrame.Position) * CFrame.Angles(math.rad(currentRotation.X), math.rad(currentRotation.Y), math.rad(currentRotation.Z)))
(Jitters the camera everywhere)
and
camera.CFrame *= camera.CFrame:ToObjectSpace(CFrame.new(camera.CFrame.Position) * CFrame.Angles(math.rad(currentRotation.X), math.rad(currentRotation.Y), math.rad(currentRotation.Z)))
(Locks camera in place)
For rotating the camera along the Z-axis, you can just use *CFrame.Angles
in a RenderStepped
loop, since the camera CFrame is re-updated every frame before the default RenderStepped
call. Keep in mind this only works for the Camera’s Z-axis, which is basically the “tilt left/right” axis. Here’s an example of code:
local camera = workspace.CurrentCamera
local runService = game:GetService("RunService")
local zRotation = math.rad(20) -- angle to rotate camera
runService.RenderStepped:Connect(function(delta)
camera.CFrame *= CFrame.Angles(0,0,zRotation)
end)
I ended up subtracting the offset by what it has already completed and it seems to work, don’t know if it is a good solution or not. Seems to work quite well though.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.