Position isn't aligning correctly (using cframe)

I am trying to make a headcrab in roblox, while trying to code the headcrab to point at the target/player, I ran into a problem where the headcrab isnt perfectly aligned.

I’ve tried adding an offset, changing C0 to C1, and still couldnt get it to work.

Code:


function LookAt(lookAt, speed : number, duratation : number, offset : number)
	for i = 1, duratation * 100, 1 do
		task.wait()
		
		print(script.Parent.Head.Position)
		print(lookAt.Position)
		
		local lookAtFormula = CFrame.new(script.Parent.HumanoidRootPart.Position + offset, lookAt.Position)
		
		local formula = lookAtFormula * CFrame.Angles(-lookAtFormula.Rotation.X,0,0)		
		hc_movement_joint.C0 = hc_movement_joint.C0:Lerp(formula,speed)
		
	end
end

print(hc_movement_joint.C0)

task.wait(3)

local offset = script.Parent.HumanoidRootPart.CFrame:ToObjectSpace(script.Parent.Head.CFrame).Position

print(offset)

LookAt(game.Workspace.lookAtPart, .4, 40, offset)`
type or paste code here

Edit: forgot the explorer, here:

image

Help is appriciated!

can you show a screenshot of what it is currently doing and what you want it to do?
or something like that?

Is it that you just want it to ‘look’ in the direction of the player?

This is what the position of the headcrab looks like when running:


This is how I want the position to be:

In both images, the head crab is ‘looking’ facing towards, the block.
So are you not just wanting it to look at the part, but are you wanting it to move to where it is lined up with it?

The issue is that when I run, the headcrab moves to a different position, the looking is fine, but it moves everytime I run, heres more pictures, but this time the HumanoidRootPart is visible.

Expectation:


Reality:

how are you moving the headcrab, is it only by changing the joint’s c0? What is that joint connected to?

if you want to move the headcrab, you really should use :PivotTo, on the model of the headcrab

the joint’s C0 is the CFrame of the headcrab, also the reason why I didn’t think of using PivotTo is because I’ve never used it, I’ll be sure to look at the api for that.

I’ve been on this for a while now, I don’t really know how to PVInstances and PivotTo, I’ve been trying to fix it with PivotTo, and still can’t find out how to fix it, let me try to explain it better;

in the studio, (before the game starts) the position is how I want it to be, when I run it, the script thinks that I want the position to be somewhere else, so it goes to wherever it thinks it wants to go instead the position I want to send it to.

So what exactly is the head crab supposed to do.
Is it supposed to …

  1. Stay in one location (where you have it placed at spawn) and just look at the player, when the player moves by.
  2. Move towards the player
  3. Stay a certain distance from the player, as the player moves?

I will type up a little bit of code, and see if it helps you.

image

Before I run it…
image

After I run it… (HurmanoidRootPart in blue)
image

function LookAt(source,target)
	source:PivotTo(CFrame.lookAt(source:GetPivot().Position,target:GetPivot().Position))
end
task.wait(3)

LookAt(workspace.Model,workspace.lookAtPart)

Forgot about this topic, I fixed it with ToPivot(),

Code (if anyone wants it)

function LookAt(lookAt, speed : number, duratation : number)
	for i = 1, duratation * 100, 1 do
		task.wait()
				
		local formula = CFrame.new(script.Parent.HumanoidRootPart.Position, lookAt.Position)
		
		local newPos = script.Parent.PrimaryPart.CFrame:Lerp(formula,speed)
		
		local currentOrientation = script.Parent.HumanoidRootPart.Orientation
		
		script.Parent:PivotTo(newPos)
		
--		script.Parent.HumanoidRootPart.Rotation = Vector3.new(0,currentOrientation.Y,currentOrientation.Z)
		
	end
end

task.wait(3)

LookAt(game.Workspace.lookAtPart, .4, 40)

Note: I had to set the headcrab model to the HumanoidRootPart.