Howdy Robloxians,
I’m not an expert at CFrame math and I’ve had a tough time trying to figure out how I can rotate a part around its own local Y axis as it is offset from a raycast’s hit normal.
The main issue that I need to fix is this part of the code:
VisualPart.CFrame *= CFrame.Angles(0, math.rad(10), 0)
Although this works, it only works for when the part is on a flat surface. As seen in the videos below it makes the part rotate around its Y axis, but as the part’s orientation changes due to the raycast normal offset, it begin as to rotate around its Y axis, but in its own World Space which makes the part rotate in a wonky way which is not wanted.
The video below shows what the part looks like without any rotational transformations, the part offsets its own orientation to match the raycast’s normal. What I want the part to do for it to rotate also around its Y axis by a set increment (it being as of now math.rad(10)).
The video below shows how the rotation on the part is constant, but as its offset from the raycast changes due to the terrain’s elevation, the rotation of the part itself does not rotate along its own Y axis, but rotates all of the part’s axes by the world space CFrame of the part
The video below shows how when I turn the camera, the part follows along with it, but its rotation remains unwanted
If you are wondering, yes I have read through hundreds of other forum post’s solutions, which none have helped to fix this.
Feel free to give your feedback or suggestions on how I could fix this, thank you!
Source Code:
RotationPart.lua (1.6 KB)
Source code without downloading the .lua file
-- Local script in player
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local RayLength = 50
-- Makes the visual part you can see
local function rayVisual(color)
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.CanQuery = false
part.Color = color
part.Transparency = .5
part.Size = Vector3.new(2,5,2)
part.Name = "raypart"
part.Parent = workspace
return part
end
local VisualPart = rayVisual(Color3.fromRGB(255,0,0))
wait(1)
game:GetService("RunService").Stepped:Connect(function()
local LookVec = Camera.CFrame.RightVector:Cross(-Player.Character.HumanoidRootPart.CFrame.UpVector)
local Origin = (Player.Character.HumanoidRootPart.CFrame + (LookVec * RayLength/3)).Position + Vector3.new(0,RayLength/1.5,0)
local Direction = -Player.Character.HumanoidRootPart.CFrame.UpVector * RayLength
local newRay = workspace:Raycast(Origin, Direction)
if newRay then
-- Positions the part based off the normal in which the ray hits
VisualPart.CFrame = CFrame.new(newRay.Position, newRay.Position + newRay.Normal:Cross(VisualPart.CFrame.RightVector))
-- Main part for of the code which I am trying to rotate the part based off its own local y axis
VisualPart.CFrame *= CFrame.Angles(0, math.rad(10), 0)
end
end)