Help with adding a pivot rotation to a part with cframe.fromMatrix

Hey guys I am working on an aim system in which the mouse is the pointer and the route of the proyectile is defined by the character humanoidrootpart position and the mouse position

The thing is that I might use CFrame.lookat so the route part always looks at the mouse but the face part is not correctly assigned and so I must use CFrame.fromMatrix but at the same time I must use pivot rotation so the route always starts in at humanoid rootpart position.

This is the code:

local center = workspace.Aim.Center
local pointer = workspace.Point

local playerChar = game.Players.LocalPlayer.Character.HumanoidRootPart
local unit

local mouseplay = game.Players.LocalPlayer:GetMouse()

game:GetService("RunService").Heartbeat:Connect(function()
	
	pointer.Position = Vector3.new(mouseplay.Hit.X, mouseplay.Hit.Y, 4)
	center.Position = playerChar.Position

	unit = (pointer.Position-center.Position).Unit
	center.CFrame = CFrame.fromMatrix(center.Position, unit, Vector3.new(0,0,1))
	
end)

Current Aim System (Not desired):

Want To Achieve:

What can I do to add a PivotRotation to CFrame.fromMatrix (with CFrame.lookAt)
pls help :wink:

1 Like

When you right-multiply a rotation by another CFrame, it rotates the left CFrame around its current position. So, you just need to do the rotation after you’ve set the position.

such as:

cylinderCframe = CFrame.new(cylinderPivotPointInCylinderSpace) * rotationCFrameAroundZero
1 Like

thanks for the support but can u type the code literally cuz i dont really understand, like what is CFrame Around Zero i would appreciate that + its neccesary to use cframe.fromMatrix or similar

Around zero just means a CFrame whose translation component is zero. You can turn any cframe into only a rotation around zero by subtracting its own position: cf = cf - cf.Position

When you create a CFrame by rotating first and then translating, its like you are rotating the axes and then sliding it along those axes. When you Translate first then Rotate, it is like you are setting the pivot point and then rotation. To make the CFrame you want, you must first Translate to the pivot point, then multiply your desired rotation, then translate from your pivot point to your final position. There are other ways to get this same result. I would personally not recommend fromMatrix, it’s just slightly less clear, even though you could make it give that result.

To be extra clear just in case: switching the sides of a matrix multiply gives different results, unlike multiplying regular numbers.

1 Like

What are other ways without using fromMatrix, because I need this part to look to another part. And I dont know any other method but lookAt and fromMatrix

You can and should do it with lookAt, since its the most descriptive function for what you want, which is literally for the part to look at a position. Since you’re aiming a cylinder, lookAt aims the Z axis of the part at the target, but cylinders have their circular face on their X axis, you must also do this rotation as the final step: finalCFrame = lookAtCFrame * CFrame.fromAxisAngle(Vector3.yAxis, math.pi/2)

1 Like

Ok , but for now, am i on the right track?
it is working as when i was using fromMatrix

game:GetService("RunService").Heartbeat:Connect(function()
	
	pointer.Position = Vector3.new(mouseplay.Hit.X, mouseplay.Hit.Y, 4)
	center.Position = playerChar.Position
	
	
	center.CFrame = CFrame.new(center.PivotOffset:Inverse() * center.Position) * CFrame.new(center.CFrame.p - center.Position) * CFrame.lookAt(center.Position, pointer.Position) * CFrame.fromAxisAngle(Vector3.yAxis, math.pi/2)
end)

Your call to lookAt is not a rotation around zero since the first argument is not zero. You can fix this by subtracting center.Position from both arguments. Im not sure whats going on with the first two CFrames in that line. Can you draw what you believe it is doing so I can check?

This code:


	center.CFrame =  CFrame.new(center.CFrame.p - center.Position) * CFrame.lookAt(center.Position, pointer.Position) * CFrame.fromAxisAngle(Vector3.yAxis, math.pi/2)

Produces a partial result (what is missing is that the cylinder rotates with pivot of center of humanoid.

Put a translation at the very end of that:

center.CFrame =  CFrame.new(center.CFrame.p - center.Position) * CFrame.lookAt(center.Position, pointer.Position) * CFrame.fromAxisAngle(Vector3.yAxis, math.pi/2) * CFrame.new(Vector3.xAxis * offset)
1 Like

omg thanks a lot :hugs: you made my day tyyy

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