CFrame.LookAt() with Motor6D(Making torso face mouse.Hit)

Hello, I have a gun script that works fine. However, I don’t like the idea of the bullet tracers going through the players character to hit its target. I made a simple solution to this problem though; I will make the character face its mouse.Hit position(aimpos). I personally am very bad at trig so I decided to use this script:

RootHI.C0 = CFrame.lookAt(hrp.Position, aimpos)

When I used this script, it was acting funky and teleported my character through the ground and back for some reason.

I have also tried:

hrp.CFrame = CFrame.LookAt(hrp.Position, aimpos)

This worked perfectly but when I used it, it didn’t allow my character to move when shooting because it constantly set my characters hrp position.

DO KEEP IN MIND that I have tried to use CFrame.LookAt() for the HumanoidRootPart to the aimpos but it didn’t allow my character to move when I used this. Therefor I am using Motor6Ds.

All feedback is appreciated;

Thanks,

I also figured that I would mention that I am new to CFrame animating as well as using Motor6Ds.

try
hrp.CFrame = CFrame.LookAt(hrp.Position, aimpos * Vector3.new(1, 0, 1))

I tried using your code. It gave me this error:
https://gyazo.com/624f4b68c6aaa678ee295fb3e4a00709

you have to define hrp and aimpos obviously

hrp and aimpos are my exact variables.

That’s because the C0 is its own CFrame that is different from the HumanoidRootPart CFrame.

RootHI.C0 = CFrame.lookAt(RootHI.C0.Position, aimpos)

From what I know, the C0 value would be a relative position, not a global one. I think you could do something like:

RootHI.C0 = CFrame.lookAt(RootHI.C0.Position, aimpos):ToObjectSpace(hrp.CFrame)

Or something of that nature.

robloxapp-20211219-2118573.wmv (1.9 MB)

I tried this line and this was the result.

robloxapp-20211219-2120362.wmv (1.9 MB)

I also tried yours. Something isn’t quite right here.

It’s CFrame.lookAt not CFrame.LookAt

Maybe this will work?

local RunService= game:GetService("RunService");
local BodyGyro = instance.New("BodyGyro", HumanoidRootPart);
BodyGyro.MaxTorque = Vector3.New(0, 1e6, 0);

RunService.RenderStepped:Connect(function()
	BodyGyro.CFrame = CFrame.lookAt(HumanoidRootPart.Position, aimpos);
end)
1 Like

Brilliant! I had to edit this code a little of course since it’s inside a server script but it worked nice and my character turned smooth exactly as I wanted. Since I have no expierience with Gyros I would have never thought of this. Nice Job.

Also, sorry for the some what late response.

1 Like