A way to set CFrame lookAt with an offset to the pivot?

Below is an image of a weapons system I am working on. The red cube is the intended point of aim and the orange cube is the current point of aim.

The rotation of the humanoid root part is calculated simply by the X and Z coordinates of the mouse in 3D space.

(Prototype.MPos = Vector3 mouse position)

-- Character rotation
Prototype.Character.HumanoidRootPart.CFrame = CFrame.new(Prototype.Character.HumanoidRootPart.Position,Vector3.new(
    Prototype.MPos.X,
    Prototype.Character.HumanoidRootPart.Position.Y,
    Prototype.MPos.Z
))

The weapon does point directly forward.
The weapon points slightly to the left, but the problem still remains.

Is there a way to offset the the position where the rotation is applied (pivot) on the humanoid root part and how would I go about doing that?


EDIT:
I’m gonna go into a bit more detail, but this is kinda strange so I’m hoping people will understand what the goal is.

Lemme explain through an example:

(i.e.) 
yellowObj.CFrame = CFrame.new(yellowObj.CFrame.Position, vector3ToLookAt)

the yellow part would normally apply the rotation of the lookAt position in the centre of the object…

I need to move the position where the lookAt vector rotates the part (pivots) to another point on the part.

More specifically:

The red dot is where the current pivot of lookAt is.

I need it here:

Or an alternative to using the lookAt vector that achieves the same result with the same degree of accuracy.

You can use lookat with pivoting if you do some offsetting.

Now, I’m assuming from your post you want to pivot and not acting like a point is the center.

Now, bear in mind I haven’t tested the following as much as I should have, so if there is a mistake in my logic, please call out.

local function pivoting_look_at(from, to, pivot_offset) 
	--[[ 
	Takes arguments: 
	CFrame (object to create CFrame from), 
	Vector3 (Point that the new CFrame will look at),
	Vector3 (The pivot point in relation to from.)
	
	Returns: CFrame that is looking at the given point having pivoted around an offset.
	--]]

	assert(typeof(from)=="CFrame", "'pivoting_look_at': Invalid input for 'from'. It is a " .. typeof(from) .. ". Should be a CFrame") 
	assert(typeof(to)=="Vector3", "'pivoting_look_at': Invalid input for 'to'. It is a " .. typeof(to) .. ". Should be a Vector3.")
	assert(typeof(pivot_offset)=="Vector3", "'pivoting_look_at': Invalid input for 'pivot_offset', It is a " .. typeof(pivot_offset) .. ". should be a Vector3")
	
	
	local offsetfrom = from:ToWorldSpace(CFrame.new(pivot_offset))
	
	--[[Use the lookat CFrame creation with the offset position looking at
	the "Worldly" similar offset to-position.
	Effectively 'pivoting around a point']]--
	local ret = CFrame.new(offsetfrom.Position, to + offsetfrom.Position - from.Position)

	-- Return the pivoted center and not the pivoted pivot point.
	ret = ret:ToWorldSpace(CFrame.new(-pivot_offset))
	
	return ret
end

Using your yellowblock example, to achieve what you did you would use

yellowObj.CFrame = pivoting_look_at(yellowObj.CFrame, vector3ToLookAt, Vector3.new(0, -yellowObj.Size.Y/2, 0))
1 Like

Thanks, the code works exactly how I needed it to.

1 Like