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
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.
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.
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.