SelDraken
(SelDraken)
February 3, 2025, 3:41am
#1
I am in need of snapping a cframe rotation to the nearest 90 degrees on a particular axis
I am placing an object, and I have a worldPosition, and I have my charactersposition
So, I can get a cframe rotated to look at the position of my character, and I can remove the x and z rotation, so its only rotating around the Y
However, I am needing the Y to be snapped to the nearest rotation in 90 degree increments.
I searched all over, and I can only find cframe snapping tutorials for position, but I need rotational snapping. Thanks.
sonic_848
(killer)
February 3, 2025, 4:08am
#2
So you just need this object to basically to be rotating around object Y in 90 degree increments on the Y-axis?
SelDraken
(SelDraken)
February 3, 2025, 4:10am
#3
sort of, it will have a y rotation, that is making the block face the player, but I then need to take that y rotation and snap it to the nearest 90 degrees, whichever it is closest to… 0, 90, 180 or 270
math.round(rotation / 90) * 90
SelDraken
(SelDraken)
February 3, 2025, 4:26am
#5
how would I single out a single rotation. Such as, how would I read out that Y, modify it, and put it back into the cframe?
sonic_848
(killer)
February 3, 2025, 4:27am
#6
Ah I see… All you need is to use a bit of Trigonometry then
Example:
local BlockA;
local BlockB;
local Angle = 90
local Radius = 1
BlockB.CFrame = BlockA.CFrame * CFrame.new(Vector3.new(math.cos(math.rad(Angle)) * Radius, 0, math.sin(math.rad(Angle)) * Radius))
Edit: To change axis of rotation just switch where the zero is in the CFrame.new
. For example to switch onto the next axis it would be 0, cos, sin or cos, sin, 0
SelDraken
(SelDraken)
February 3, 2025, 4:35am
#7
So the 0 represents the axis I am wanting to rotate around?
Also, it didn’t make any changes, but maybe im plugging it into my code wrong, let me prepare a snippet.
SelDraken
(SelDraken)
February 3, 2025, 4:42am
#8
Hopefully this helps see what I’m doing wrong.
local pos = raycastResult.Position - (rayDirection*.01)
local cell = Vector3.new(pos.X//BlockSize,pos.Y//BlockSize,pos.Z//BlockSize)
local cf = CFrame.new((cell.X+.5)*BlockSize,(cell.Y+.5)*BlockSize,(cell.Z+.5)*BlockSize) --CFrame.new( (cell.X*BlockSize)+(BlockSize/2),(cell.Y*BlockSize)+(BlockSize/2),(cell.Z*BlockSize)+(BlockSize/2) )
local Character = game.Players.LocalPlayer.Character
local target = (Character.PrimaryPart.Position * Vector3.new(1,0,1) ) + Vector3.new(0,cf.Position.Y,0)
local cf2 = CFrame.lookAt(cf.Position,target,Vector3.yAxis)
local Angle = 90
local Radius = 1
local cf3 = cf2 * CFrame.new(Vector3.new(math.cos(math.rad(Angle)) * Radius, 0, math.sin(math.rad(Angle)) * Radius))
if DistanceCheck(cf) then
Block:PivotTo(cf3)
Highlight.Adornee = Block
Block.Parent = Player.Character --workspace
valid = true
end
sonic_848
(killer)
February 3, 2025, 4:43am
#9
0 on Y = spin on x axis
0 on X = spin on z axis
0 on Z = spin on y axis
Lemme read above one
SelDraken
(SelDraken)
February 3, 2025, 4:47am
#10
I still get non 90 deg angles with the above code
sonic_848
(killer)
February 3, 2025, 4:55am
#11
To be honest you could just use its rotation property if it’s just a mesh.
Something like:
local Mesh;
mesh.Rotation = Vector3.new(0,90,0)
Edit: If you want it aligned with the world. I am not quite sure what you’re trying to align it relative to
SelDraken
(SelDraken)
February 3, 2025, 4:58am
#12
Oh no, that last reply was a ‘ok, I’ve given up on you’ reply XD
If you see in the picture above, how the rotation is facing the character, I need it to do that, but then ‘snap’ to the closest 90 degrees, so it should look something like this…
sonic_848
(killer)
February 3, 2025, 5:09am
#13
maybe try this:
local PartA = Your stairs
local PartB = Your Humanoid Root Part
local Angle = 90
local Radius = 5
PartA.Position = PartB.Position + Vector3.new(math.cos(math.rad(Angle)) * math.abs(Radius), 0, math.sin(math.rad(Angle)) * math.abs(Radius));
PartA.CFrame = CFrame.lookAt(PartA.Position,PartB.Position)
SelDraken
(SelDraken)
February 3, 2025, 5:16am
#14
Still not giving me the correct results, thanks for the help, im off to bed now. I appreciate all the help, I’ll tackle this some more tomorrow.
sonic_848
(killer)
February 3, 2025, 5:25am
#15
Ok… Idk what you did wrong but it works for me. It rotates around me in 90-degree intervals and faces me. Unless you wanted something else too…
Photos:
The stairs are raised up cz it matches HRP height. This can be reduced so it’s on the ground
SelDraken
(SelDraken)
February 3, 2025, 5:37am
#16
Yeah that’s exactly what I need. I’m probably just too tired to know what I’m doing right now.
Thanks again
sonic_848
(killer)
February 3, 2025, 5:40am
#17
If you have any problems tomorrow let me know and I’ll help you out… Gn
system
(system)
Closed
February 17, 2025, 5:41am
#18
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.