I am trying to make my dinosaur rigs’ head move when you look around using the camera.
So far I have very little to no scripting experience, but from what I have seen, I think I should be using something like ‘mouse.Hit.p’ ?
I have come across some scripts in the library that move the entire body, however I am focusing on moving just the head.
Does anybody know how I can do this?
Update:
If nobody knows how this can be done, is there a way to play an animation for looking left, forward, right depending on where the player looks?
Somebody sent me a private message asking me how I did this in my own game. The following is my response.
Hopefully it can give you a decent place to start.
(If you can un-heck this code I wrote when I barely knew what I was doing, be my guest.)
The way that I’m doing it is somewhat unique, is definitely not perfect, and has some quirks.
I have four animations for each direction the head can turn, and use the camera direction to adjust the weight of each animation according to where the camera is pointing.
I do it this way because attempting to do this by manually manipulating the motors would be a lot of work since I have many creatures of different sizes, they may or may not have more than one neck segment, and iirc that kind of manual animation wouldn’t replicate.
Some issues:
If I update the animation weight too frequently, the entire head might be sent to NaN,NaN,NaN, which is not very user-friendly.
Sometimes the resting position for the head is not centered - it depends on the order you initially play the four animations. Sometimes playing Right before Left fixes this issue.
The script:
Beware, this is somewhat old code.
This is a LocalScript.
The script expects four Animation instances as its children, named Up, Down, Left, and Right.
The commented out block is used for creatures that have a dedicated neck segment, and not just a head on the body. You may not need it at all. It may cause weird behavior. Who knows.
-- INIT
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local root = character:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera
-- ANIM SETUP
local up = script:WaitForChild("Up")
local down = script:WaitForChild("Down")
local left = script:WaitForChild("Left")
local right = script:WaitForChild("Right")
local animator = humanoid:WaitForChild("Animator")
local upAnim = animator:LoadAnimation(up)
local downAnim = animator:LoadAnimation(down)
local leftAnim = animator:LoadAnimation(left)
local rightAnim = animator:LoadAnimation(right)
downAnim:Play(0.4, 0, 1)
upAnim:Play(0.4, 0, 1)
leftAnim:Play(0.4, 0, 1)
rightAnim:Play(0.4, 0, 1)
-- FUNCTIONS
local function adjust()
local rootCF = root.CFrame
local lookP = rootCF:toObjectSpace(cam.CFrame).lookVector
local r = math.abs((lookP.x+1)/2)
local l = 1-r
local u = math.abs((lookP.y+1)/2)
local d = 1-u
-- NEEDED ONLY IF ANIMATION USES ALL PARTS OF NECK
--[[
if (l < 0.6 and l > 0.4) then
l = l - 0.5
r = r - 0.5
end
]]
upAnim:Play(0.4, u, 1)
downAnim:Play(0.4, d, 1)
leftAnim:Play(0.4, l, 1)
rightAnim:Play(0.4, r, 1)
end
-- MAIN
while wait(0.2) do
adjust()
end