generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Getting Head to Follow Cursor

Previous Thread :: Next Thread 
PhokiusV3 is not online. PhokiusV3
Joined: 07 May 2013
Total Posts: 805
20 Dec 2015 05:59 PM
I'm experimenting with trigonometric functions to tackle writing a script or routine that can adjust in general the angles of welds to have an adorned part in said weld produce an angle such that its lookvector coincides with the desired relative angle between the main part of said weld and its adorned part.

My first application to the idea is to have a player's head face the cursor.

So far, and without avail, is my work that I need help with:

-------------------------------------------------------------------

local render = game:GetService("RunService").Stepped

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local head, neck, oldNeckC0 = player.Character.Head, player.Character.Torso.Neck, player.Character.Torso.Neck.C0

-- Doing this at about 30Hz
render:connect(function ()

--Get the isolated unit vectors for the head relative to the game's coordinates
local PitchHeadLV, YawHeadLV = head.CFrame.lookVector*Vector3.new(0, 1, 0), head.CFrame.lookVector*Vector3.new(1, 0, 0)

--Do the same for the mouse position, relative to the game's coordinates
local PitchMouseLV, YawMouseLV = CFrame.new(head.CFrame.p, mouse.Hit.p).lookVector*Vector3.new(0, 1, 0), CFrame.new(head.CFrame.p, mouse.Hit.p).lookVector*Vector3.new(1, 0, 0)


-- Do some fancy trigonometry with the vectors to find the thetas between the vectors
local PitchAngle, YawAngle = math.acos(PitchHeadLV:Dot(PitchMouseLV)), math.acos(YawHeadLV:Dot(YawMouseLV))

-- Rotate the joint about the thetas, I don't care for Yaw right now
neck.C0 = oldNeckC0 * CFrame.Angles(0, PitchAngle, 0)
end)

-------------------------------------------------

Any help would be greatly appreciated. When my fully written routine is complete, it could change a lot of things.
Report Abuse
PhokiusV3 is not online. PhokiusV3
Joined: 07 May 2013
Total Posts: 805
20 Dec 2015 06:28 PM
Bring
United
Master
Pizzas
Report Abuse
101airsoft is not online. 101airsoft
Joined: 11 Mar 2012
Total Posts: 145
20 Dec 2015 06:37 PM
local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
local camera=workspace.CurrentCamera

mouse.Idle:connect(function()
local char=player.Character
if (char) then
char.Torso["Neck"].C1=CFrame.new()
char.Torso["Neck"].C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), 0, 0)
end end)
camera.Changed:connect(function()
local char=player.Character
if (char) then
char.Torso["Neck"].C1=CFrame.new()
char.Torso["Neck"].C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), 0, 0)
end end)
Report Abuse
101airsoft is not online. 101airsoft
Joined: 11 Mar 2012
Total Posts: 145
20 Dec 2015 06:39 PM
--Or you could put this one in starter pack or starter gui
local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
local camera=workspace.CurrentCamera
repeat wait() until(player.Character)
local neck=player.Character.Torso:FindFirstChild("Neck")

mouse.Idle:connect(function()
neck.C1=CFrame.new()
neck.C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), 0, 0)
end)
camera.Changed:connect(function()
neck.C1=CFrame.new()
neck.C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), 0, 0)
end)
Report Abuse
PhokiusV3 is not online. PhokiusV3
Joined: 07 May 2013
Total Posts: 805
20 Dec 2015 06:43 PM
That actually works very nice.
But how would I get to rotating the head around its axis to fully face the cursor?
Report Abuse
XIPokezIX is not online. XIPokezIX
Joined: 24 Sep 2015
Total Posts: 381
20 Dec 2015 06:43 PM
^^is that more smoother than a while loop?
Report Abuse
101airsoft is not online. 101airsoft
Joined: 11 Mar 2012
Total Posts: 145
20 Dec 2015 06:55 PM
local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
local camera=workspace.CurrentCamera
repeat wait() until(player.Character)
local neck=player.Character.Torso:FindFirstChild("Neck")

mouse.Idle:connect(function()
neck.C1=CFrame.new()
neck.C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), -math.asin((mouse.Hit.p-mouse.Origin.p).unit.x), 0)
end)
camera.Changed:connect(function()
neck.C1=CFrame.new()
neck.C0=CFrame.new(0, 1.5, 0)*CFrame.Angles(math.asin((mouse.Hit.p-mouse.Origin.p).unit.y), -math.asin((mouse.Hit.p-mouse.Origin.p).unit.x), 0)
end)
Report Abuse
PhokiusV3 is not online. PhokiusV3
Joined: 07 May 2013
Total Posts: 805
20 Dec 2015 07:02 PM
I've gotten the same results with the last iteration of my attempts.
It functions, but it doesn't properly follow the cursor.
this is why i'm trying to write a routine that adjusts the weld according to the current lookvector in difference with the lookvector of the mouse cursor.

Thanks for trying though.
Report Abuse
nox7 is not online. nox7
Joined: 29 Aug 2008
Total Posts: 27467
20 Dec 2015 07:22 PM
What in blazes are you guys doing.

Neck.C1 = (CFrame.new(Head.Position, Mouse.Hit.p)):toObjectSpace(Torso.CFrame * Neck.C0)
Report Abuse
DevelopmentStatus is not online. DevelopmentStatus
Joined: 22 Apr 2015
Total Posts: 235
20 Dec 2015 07:55 PM
Make a local script and put it in StarterGui

repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:FindFirstChild("Head")
local Torso = Character:FindFirstChild("Torso")
local Neck = Torso:FindFirstChild("Neck")
local NeckOrigin = Neck.C1
local RS = game:GetService("RunService")
local Mouse = Player:GetMouse()

RS.RenderStepped:connect(function()
IgnoreHeadChange = true
local LastHeadRotation = Neck.C1
Neck.C1 = NeckOrigin
local HeadOrigin = Head:Clone()
local Base = Neck.Part0.Position + (Neck.C0.p * 1)
local YVector = (Mouse.Hit.p + Mouse.Hit.lookVector)
local Set = CFrame.new(Base, YVector)
local Offset = Set:toObjectSpace(Torso.CFrame * Neck.C0)
local PosX, PosY, PosZ, R00, R01, R02, R10, R11, R12, R20, R21, R22 = Offset:components()
Neck.C1 = CFrame.new(NeckOrigin.p.x, NeckOrigin.p.y, NeckOrigin.p.z, R00, R01, R02, R10, R11, R12, R20, R21, R22)
local HeadRotationCheck = false
if math.abs((Head.Position - Torso.Position).magnitude) >= 1.25 then
HeadRotationCheck = true
else
Neck.C1 = LastHeadRotation
end
IgnoreHeadChange = false
if HeadRotationCheck and (math.acos((Mouse.Hit.p - HeadOrigin.Position).Unit:Dot(HeadOrigin.CFrame.lookVector)) <= (math.pi / 2)) then
NeckChangedConnections = {}
for i, v in pairs(NeckChangedConnections) do
v:disconnect()
end
local NeckChanged
NeckChanged = Neck.Changed:connect(function(Property)
if Property == "C0" or Property == "C1" and not IgnoreHeadChange then
for i, v in pairs(NeckChangedConnections) do
v:disconnect()
end
NeckChangedConnections = {}
end
end)
table.insert(NeckChangedConnections, NeckChanged)
Delay(1, function()
if NeckChanged.connected then
Neck.C1 = Neck.C1:lerp(NeckOrigin,0.3)
end
end)
else
IgnoreHeadChange = true
Neck.C1 = LastHeadRotation
IgnoreHeadChange = false
end
end)
Report Abuse
DevelopmentStatus is not online. DevelopmentStatus
Joined: 22 Apr 2015
Total Posts: 235
20 Dec 2015 08:01 PM
I must give credit to Luckymaxer, almost the whole script was made by him, cut directly from Mind Controlled Lasers, I converted it into a local script.

I made this a separate post of its own just so its more noticeable.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image