Snap CFrame Rotation to nearest degree

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.

So you just need this object to basically to be rotating around object Y in 90 degree increments on the Y-axis?

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

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?

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

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.

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

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

I still get non 90 deg angles with the above code
image

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

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…

image

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)

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.

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:
image
image
image
image

The stairs are raised up cz it matches HRP height. This can be reduced so it’s on the ground

Yeah that’s exactly what I need. I’m probably just too tired to know what I’m doing right now.
Thanks again

If you have any problems tomorrow let me know and I’ll help you out… Gn

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.