Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help with a R15 Mouse-to-Character-Movement?

Asked by 7 years ago

How could I make this so when you move the mouse, your character rotates to where the mouse is.

So the first script is a normal script and it is located in "StarterCharacterScripts" :

local remote = Instance.new("RemoteEvent", script);
remote.Name = "Remote";

remote.OnServerEvent:connect(function(player, mouseDirection, neck)
    neck.C1 = CFrame.new(Vector3.new(0, -0.5, 0)) * CFrame.Angles(-math.asin(mouseDirection.y), 0, 0)
end)

After that, the second script (local) is in the first script, and here it is :

local player = game.Players.LocalPlayer;
local runService = game:GetService("RunService").Heartbeat;
local playerMouse = player:GetMouse();
local remote = script.Parent:WaitForChild("Remote");
local character = player.Character;
local humanoid = character:FindFirstChild("Humanoid");
local head = character:FindFirstChild("Head");
local neck = head ~= nil and head:FindFirstChild("Neck") or nil;

local lastMousePosition = playerMouse.UnitRay.Direction.unit;
runService:connect(function()
    if head ~= nil then
        if neck ~= nil and lastMousePosition ~= playerMouse.UnitRay.Direction.unit then
            remote:FireServer(playerMouse.UnitRay.Direction.unit, neck);
            lastMousePosition = playerMouse.UnitRay.Direction.unit;
        else
            neck = head ~= nil and head:FindFirstChild("Neck") or nil;
        end
    else
        head = character:FindFirstChild("Head");
    end
end)

What this script does, it makes the head follow the mouse for a R15 character.

Thanks for your help!

1 answer

Log in to vote
0
Answered by 7 years ago

I used BodyGyro since it's way smoother than CFraming (at least that I could find) and inserted this code in a local script

--CodeOverload
--Point at mouse

game.Players.LocalPlayer.CharacterAdded:connect(function(char)
    local mouse = game.Players.LocalPlayer:GetMouse()
    local root = char.HumanoidRootPart
    local gyro = Instance.new('BodyGyro', char.HumanoidRootPart)
    gyro.MaxTorque = Vector3.new(100000000, 100000000, 100000000)
    gyro.P = 100000000
    while wait() do
        gyro.CFrame = CFrame.new(root.Position, Vector3.new(CFrame.new(mouse.Hit.p).X, root.CFrame.Y, CFrame.new(mouse.Hit.p).Z))
    end
end)
0
Where did you inserted this local script? starchip12 6 — 7y
0
nvm I found it, thanks for the help :D ! starchip12 6 — 7y
Ad

Answer this question