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

Head moving the opposite way in y axis?

Asked by
Hero_ic 502 Moderation Voter
9 years ago

The problem is on my shoulder cam script the arms move down with the cam but the head goes the opposite way I do not know how to fix this any help? please scroll down until you see the problem in the neck.c0.cframe at the bottom of the code

wait(2)

local InputService=game:GetService("UserInputService")
local Camera=game.Workspace.CurrentCamera
local Player=game.Players.LocalPlayer
local Character=Player.Character
local Head=Character.Head
local Torso=Character.Torso
local RootPart=Character.HumanoidRootPart
local RootJoint=RootPart.RootJoint
local Neck=Torso.Neck
local RightShoulder = Torso:WaitForChild("Right Shoulder")
local LeftShoulder = Torso:WaitForChild("Left Shoulder")
--Camera.FieldOfView=100
Camera.CameraType="Scriptable"
InputService.MouseBehavior=Enum.MouseBehavior.LockCenter



local v3=Vector3.new
local cf=CFrame.new
local components=cf().components
local inverse=cf().inverse
local fromAxisAngle=CFrame.fromAxisAngle
local atan,atan2=math.atan,math.atan2
local acos=math.acos

local function toAxisAngleFromVector(v)
    local z=v.z
    return z*z<0.99999 and v3(v.y,-v.x,0).unit*acos(-z) or v3()
end

local function AxisAngleLookOrientation(c,v,t)--CFrame,Vector,Tween
    local c=c-c.p
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end

local function AxisAngleLookNew(v,t)--CFrame,Vector,Tween
    local rv=v.unit
    local rz=rv.z
    return rz*rz<0.99999 and fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or cf()
end

local function AxisAngleLook(c,v,t)--CFrame,Vector,Tween
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end




local Sensitivity=0.005


local CameraDirection=Vector3.new(0,0,1)

local function EulerAnglesYX(l)
    local x,z=l.x,l.z
    return atan(l.y/(x*x+z*z)^0.5),-atan2(x,-z)
end

local function AnglesXY(l)
    local z=l.z
    return atan2(l.y,-z),-atan2(l.x,-z)
end

local function MouseMoved(Input)
    if Input.UserInputType==Enum.UserInputType.MouseMovement then
        local dx,dy=Input.Delta.x*Sensitivity,Input.Delta.y*Sensitivity
        local m2=dx*dx+dy*dy
        if m2>0 then
            CameraDirection=(AxisAngleLookOrientation(RootPart.CFrame,CameraDirection)*fromAxisAngle(v3(-dy,-dx,0),m2^0.5)).lookVector
        end
        local RootOrientation=RootPart.CFrame-RootPart.Position
        local RelativeDirection=RootOrientation:inverse()*CameraDirection
        local AngX,AngY=AnglesXY(RelativeDirection)--RootOrientation:inverse()*
        if AngX<-1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(-1.57*11/12-AngX),-math.sin(-1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        elseif AngX>1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(1.57*11/12-AngX),-math.sin(1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        end
    end
end

local Mouse=Player:GetMouse()

local Zoom=-0.5

Mouse.KeyDown:connect(function(k) 
    if k=="e" then  
        Zoom=-0.5
    elseif k=="q" then
        Zoom=8
    end
end)

InputService.InputChanged:connect(MouseMoved)

--Neck.C1=cf()
--LeftShoulder.C1=cf()
--RightShoulder.C1=cf()
local _
local DirectionBound=3.14159/3
local CurrentAngY=0

local function CameraUpdate()
    Camera.CameraType="Scriptable"
    local cx,cz=CameraDirection.x,CameraDirection.z
    local rvx,rvz=RootPart.Velocity.x,RootPart.Velocity.z
    if rvx*rvx+rvz*rvz>4 and cx*rvx+cz*rvz<-0.5*(cx*cx+cz*cz)^0.5*(rvx*rvx+rvz*rvz)^0.5 then
        DirectionBound=math.min(DirectionBound*0.9,math.abs(CurrentAngY*0.9))
    else
        DirectionBound=DirectionBound*0.1+3.14159/3*0.9
    end
    local AngX,AngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
    if AngY>.01 then
        RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY-.01,0)
    elseif AngY<-.01 then
        RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY+.01,0)
    end
    _,CurrentAngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
    local CameraOrientation=AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,.7)
    Neck.C0=CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)*CameraOrientation* CFrame.Angles(0, 0, 0) -- This = problem I can't seem to get the Cframe correctly 
    RightShoulder.C0=CFrame.new(1,.5,0)*CameraOrientation* CFrame.Angles(0, math.pi / 2, 0)
    LeftShoulder.C0=CFrame.new(-1,.5,0)*CameraOrientation* CFrame.Angles(0, math.pi / -2, 0)
    local PreCam=AxisAngleLook(RootPart.CFrame*cf(0,1,0),RootPart.CFrame*v3(0,1,0)+CameraDirection)*CFrame.new(3,0.825,0)
    local Part,Position=Workspace:findPartOnRay(Ray.new(PreCam.p,PreCam.lookVector*-5),Character)
    Camera.CoordinateFrame=PreCam*CFrame.new(0,0,(Position-PreCam.p).magnitude)
    --Camera.CoordinateFrame=PreCam*CFrame.new(0,0,Zoom)
end

game:GetService("RunService").RenderStepped:connect(CameraUpdate)

1 answer

Log in to vote
2
Answered by 9 years ago

Since the arms worked normally and the head was rotating the exact opposite way, the rotation of the head must be inverted in order to get it to rotate correctly.

Copy the value of the CameraOrientation variable and then delete that variable line. Create two variables instead of the CameraOrientation variable, one called CameraHeadOrientation and CameraArmOrientation.

The CameraArmOrientation property should be set to the value of CameraOrientation and the CameraHeadOrientation should be the same, except where the .7 is at near the end of the function call, you want to make it negative.

Making the .7 negative essentially inverts the orientation. The function multiplies the inverse cosine (which is the angle of rotation in radians) calculated in your function by the tween argument, so if you inverse the tween argument and multiply that by the inverse cosine, the inverse cosine angle will essentially be inverted, as opposed to the arm angle.

What you do then is replace CameraOrientation for the neck, right shoulder and left shoulder's C0. CameraHeadOrientation should be the replacement for CameraOrientation in the Neck's C0 and CameraArmOrientation should be the replacement for CameraOrientation in the remaining left and right shoulder C0s.

Your final script will now look like this:

wait(2)

local InputService=game:GetService("UserInputService")
local Camera=game.Workspace.CurrentCamera
local Player=game.Players.LocalPlayer
local Character=Player.Character
local Head=Character.Head
local Torso=Character.Torso
local RootPart=Character.HumanoidRootPart
local RootJoint=RootPart.RootJoint
local Neck=Torso.Neck
local RightShoulder = Torso:WaitForChild("Right Shoulder")
local LeftShoulder = Torso:WaitForChild("Left Shoulder")
--Camera.FieldOfView=100
Camera.CameraType="Scriptable"
InputService.MouseBehavior=Enum.MouseBehavior.LockCenter



local v3=Vector3.new
local cf=CFrame.new
local components=cf().components
local inverse=cf().inverse
local fromAxisAngle=CFrame.fromAxisAngle
local atan,atan2=math.atan,math.atan2
local acos=math.acos

local function toAxisAngleFromVector(v)
    local z=v.z
    return z*z<0.99999 and v3(v.y,-v.x,0).unit*acos(-z) or v3()
end

local function AxisAngleLookOrientation(c,v,t)--CFrame,Vector,Tween
    local c=c-c.p
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end

local function AxisAngleLookNew(v,t)--CFrame,Vector,Tween
    local rv=v.unit
    local rz=rv.z
    return rz*rz<0.99999 and fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or cf()
end

local function AxisAngleLook(c,v,t)--CFrame,Vector,Tween
    local rv=(inverse(c)*v).unit
    local rz=rv.z
    return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
end




local Sensitivity=0.005


local CameraDirection=Vector3.new(0,0,1)

local function EulerAnglesYX(l)
    local x,z=l.x,l.z
    return atan(l.y/(x*x+z*z)^0.5),-atan2(x,-z)
end

local function AnglesXY(l)
    local z=l.z
    return atan2(l.y,-z),-atan2(l.x,-z)
end

local function MouseMoved(Input)
    if Input.UserInputType==Enum.UserInputType.MouseMovement then
        local dx,dy=Input.Delta.x*Sensitivity,Input.Delta.y*Sensitivity
        local m2=dx*dx+dy*dy
        if m2>0 then
            CameraDirection=(AxisAngleLookOrientation(RootPart.CFrame,CameraDirection)*fromAxisAngle(v3(-dy,-dx,0),m2^0.5)).lookVector
        end
        local RootOrientation=RootPart.CFrame-RootPart.Position
        local RelativeDirection=RootOrientation:inverse()*CameraDirection
        local AngX,AngY=AnglesXY(RelativeDirection)--RootOrientation:inverse()*
        if AngX<-1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(-1.57*11/12-AngX),-math.sin(-1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        elseif AngX>1.57*11/12 then
            local y,z,c,s=RelativeDirection.y,RelativeDirection.z,math.cos(1.57*11/12-AngX),-math.sin(1.57*11/12-AngX)
            z,y=z*c-y*s,z*s+y*c
            CameraDirection=RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
        end
    end
end

local Mouse=Player:GetMouse()

local Zoom=-0.5

Mouse.KeyDown:connect(function(k) 
    if k=="e" then  
        Zoom=-0.5
    elseif k=="q" then
        Zoom=8
    end
end)

InputService.InputChanged:connect(MouseMoved)

--Neck.C1=cf()
--LeftShoulder.C1=cf()
--RightShoulder.C1=cf()
local _
local DirectionBound=3.14159/3
local CurrentAngY=0

local function CameraUpdate()
    Camera.CameraType="Scriptable"
    local cx,cz=CameraDirection.x,CameraDirection.z
    local rvx,rvz=RootPart.Velocity.x,RootPart.Velocity.z
    if rvx*rvx+rvz*rvz>4 and cx*rvx+cz*rvz<-0.5*(cx*cx+cz*cz)^0.5*(rvx*rvx+rvz*rvz)^0.5 then
        DirectionBound=math.min(DirectionBound*0.9,math.abs(CurrentAngY*0.9))
    else
        DirectionBound=DirectionBound*0.1+3.14159/3*0.9
    end
    local AngX,AngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
    if AngY>.01 then
        RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY-.01,0)
    elseif AngY<-.01 then
        RootPart.CFrame=RootPart.CFrame*CFrame.Angles(0,AngY+.01,0)
    end
    _,CurrentAngY=EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
    local CameraHeadOrientation=AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,-.7) --Since .7 rotates the opposite way, make .7 negative to invert the angle and rotate the head the correct way.
    local CameraArmOrientation=AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,.7)
    Neck.C0=CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)*CameraHeadOrientation* CFrame.Angles(0, 0, 0) --Multiply by CameraHeadOrientation.
    RightShoulder.C0=CFrame.new(1,.5,0)*CameraArmOrientation* CFrame.Angles(0, math.pi / 2, 0) --Multiply by CameraArmOrientation.
    LeftShoulder.C0=CFrame.new(-1,.5,0)*CameraArmOrientation* CFrame.Angles(0, math.pi / -2, 0) --Multiply by CameraArmOrientation.
    local PreCam=AxisAngleLook(RootPart.CFrame*cf(0,1,0),RootPart.CFrame*v3(0,1,0)+CameraDirection)*CFrame.new(3,0.825,0)
    local Part,Position=workspace:findPartOnRay(Ray.new(PreCam.p,PreCam.lookVector*-5),Character)
    Camera.CoordinateFrame=PreCam*CFrame.new(0,0,(Position-PreCam.p).magnitude)
    --Camera.CoordinateFrame=PreCam*CFrame.new(0,0,Zoom)
end

game:GetService("RunService").RenderStepped:connect(CameraUpdate)

There may be a more efficient way to do this, but this is what I could come up with that worked for me.

I hope my answer helped you. If it did, be sure to accept it.

0
Thanks so much! Hero_ic 502 — 9y
0
You're welcome. Do note as I stated at the end of the question, there may be a more efficient way of solving your problem! Spongocardo 1991 — 9y
Ad

Answer this question