Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't this go in the Y direction and how can i make it to do so?

This is a script that will make your player face the cursor. This works for the Z and X but not for the Y(aka up and down) How can i make it do so?Thanks for helping :).

local runService = game:GetService("RunService")
local players = game:GetService("Players")
local client = players.LocalPlayer
local mouse = client:GetMouse()

local character = client.Character or client.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local function faceMouse()
    humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, Vector3.new(mouse.Hit.p.X, humanoidRootPart.Position.Y, mouse.Hit.p.Z))
end

runService.Heartbeat:Connect(faceMouse)

2 comments

0

uhh change humanoidRootPart.Position.Y --> mouse.Hit.p.Y

0

Thanks!

1 answer

✓ Accepted answer

This is happening because you set the Y of the lookAt lookvector to the height of the HumanoidRootPart. This means that it will rotate the character to point towards the mouse X and Z, but still keep the same tilt angle. To fix this, you can change that Vector3 to something like this:

CFrame.new(humanoidRootPart.Position, mouse.Hit.p)

which will change to make the character look at all axes.

However, characters automatically balance themselves so that they dont tilt forwards or backwards. If you want to allow for tilting on these axes, you could set the PlatformStand property of the character's Humanoid to false.

However, this would prevent them from moving, so you might want to look into rotating the Root Motor6D instead. Using the logic from this page, you can get the equation C1 == Part1:inverse() * Part0 * C0. This means that if we want the Part1 (the torso) to have the CFrame that points to the mouse, we can calculate the C1 required for that like this:

local function FaceMouse()
	local Joint = humanoidRootPart:FindFirstChildOfClass("Motor6D") --forgot the name so searching for its class instead
	local DesiredCF = CFrame.new(Joint.Part1.Position, mouse.Hit.p)
	local RequiredC1 = DesiredCF:inverse() * Joint.Part0.CFrame * Joint.C0
	Joint.C1 = RequiredC1
end

Hopefully something like that works, I haven't tested it, but it should edit the Motor6D of the character so that it rotates the torso to face the mouse.

Hope this helps!

1 comment

0

Yeah thanks, I tried using ur first answer but that one always spazzes the 2nd i don't understand but i've found a solution. Thanks for Answearing

This discussion is preserved as it was. New answers, comments, and votes are closed.