So I want to create tool that when pressed, it will turn the characters head towards the direction of the mouse click, and once they move, it resets, How would I go about doing something like this?
There are lots of scripts you can use in the Toolbox for reference to looking at mouse location but maybe something like this?
local UserInputService = game:GetService("UserInputService")
wait(1)
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
function Look()
end
function StopLooking()
end
UserInputService.InputBegan:Connect(function(inputObject,busy)
if busy then return end
if Humanoid:GetState() ~= Enum.HumanoidStateType.Dead and inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
Look()
print("Look")
end
end)
Humanoid.Running:Connect(function(Speed)
if Speed > 0 and Looking then
StopLooking()
print("Stop")
end
end)
Humanoid.Died:Connect(StopLooking)
Just to clarify, on click does the head’s orientate itself to face the mouse and stay like that or does it follow the mouse’s position until the character moves?
You know how in most games, like bloxburg per say, the head turns due to camera, well I want it like thatm but instead the mouse click makes the players head and possibly uppertorso face perfectly into line with the mouse when clicked.
Which means it would have to change the neck and other torso things.
I do not know how to do that, get the right positoning, etc. Any help?
So I’ve altered my personal code to follow the mouse instead of the Camera’s lookVector, roughly thrown it together as you wanted, but please read the note at the bottom. Anyway, I hope this is the solution for this thread:
local mapCharacter = (function (character)
--[!] This just maps the welds and motor6Ds within the character for me to access later, incl. recording their C0
--[!] Not particularly necessary for yours, and it's a bit overkill, so feel free to change to variables
--[!] But it helped in my code as I changed the majority of the motors regularly
local constraints = { }
local origin = { }
local recurse; recurse = (function (o)
for i, v in next, o:GetChildren() do
if v:IsA "Motor6D" or v:IsA "Weld" then
constraints[v.Name] = v
origin[v.Name] = v.C0
end
recurse(v)
end
end)
recurse(character)
return constraints, origin
end)
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Joints, Origin = mapCharacter(Character)
local Controller = require(Player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
local InputService = game:GetService("UserInputService")
local Camera = game.Workspace.CurrentCamera
--> This is the code to actually rotate the head if you or anyone else decides to rip it
local rotateHead = (function (input)
local ray = Camera:ViewportPointToRay(input.Position.X, input.Position.Y)
local _, pos = game.Workspace:FindPartOnRay(Ray.new(ray.Origin, ray.Direction * 500))
Joints.Neck.C0 = Origin.Neck:lerp(Character.UpperTorso.CFrame:toObjectSpace(CFrame.new((Character.UpperTorso.CFrame * Origin.Neck).p, pos)), 0.5)
end)
--> Let's handle both [TouchEvents] and [MouseEvents]
if InputService.TouchEnabled then
InputService.TouchTap:connect(function (input, gameProcessed)
if gameProcessed then
return
end
rotateHead(input)
end)
else
InputService.InputBegan:connect(function (input, gameProcessed)
if gameProcessed then
return
end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
rotateHead(input)
end
end)
end
game:GetService("RunService").RenderStepped:connect(function ()
if not Character or not Character.Parent then
return
end
--> If we move and/or jump then reset the Head's original C0
if Controller:GetMoveVector().magnitude > 0 or Character.Humanoid.Jump then
Joints.Neck.C0 = Origin.Neck
end
end)
Edit: This would go inside of a Tool/LocalScript within PlayerScripts - but please note that this won’t replicate to other players unless you add a ServerSided script to do so. Don’t forget you will also need to reinitialise values if this is within a PlayerScript.
You can set the orientation of the head using a handy CFrame constructor which takes the position and the lookAt position.
head.CFrame = CFrame.new(head.CFrame.Position,mouse.Hit.Position)
Sorry that it took so long to respond, the devforum went down when I thought I made a reply.
Sorry to bump this old topic, but I though it would be worth noting in case anyone else finds this post, setting the CFrame of a character’s head will cause all sorts of issues, such as jitteriness and sliding around. Using the neck C0 is a much better approach.
Try do script.Parent.Parent.lookVector = mouse.UnitRay.Direction
From a local script where you got mouse: mouse = game.Players.LocalPlayer:getMouse()