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

Force strafing on the default player?

Asked by
drahsid5 250 Moderation Voter
8 years ago

I'm working on another third person camera, but this time I'm using ROBLOX's default player. I got it working great, but I want to force the player to strafe, rather than turn left or right. Lock first person didn't work, (I'm using lock center to make the mouse stay centered) I haven't been able to find any info on how to force the player to strafe, other than using first person.

Does anyone know how I would do this?

My code:

--Third person camera stuff--

repeat wait() until game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
--Set up--
ip = game:GetService("UserInputService")
cam = game.Workspace.CurrentCamera
plr = game.Players.LocalPlayer.Character
plr.Humanoid.AutoRotate = false

--This would be an example of forced settings, though I made a GUI settings example.
--[[
msense = 10 --Might want to make a user controllable value. I personally prefer 10x but I'm also a very fast paced gamer.
msmooth = 35 --How much is the mouse.Delta divided by?
zoomMax = 80 --The highest the camera can me rotated on the y axis, so you can't glitch it. 85 is exavtly 180 degrees.
]]--

--Coroutines--

camCnt = coroutine.wrap(function()
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CoordinateFrame = CFrame.new(plr.Torso.Position+Vector3.new(2,2,3))
    ip.MouseBehavior = Enum.MouseBehavior.LockCenter
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson   

    local angleX = 0
    local angleY = 0

ip.InputChanged:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
        if inputObject.Delta.x < 0 then
            angleX = angleX - (1 - inputObject.Delta.x/msmooth) * msense
        elseif inputObject.Delta.x > 0 then
            angleX = angleX + (1 + inputObject.Delta.x/msmooth) * msense
        end
        if inputObject.Delta.y < 0 then
            angleY = angleY - (1 - inputObject.Delta.y/msmooth) * msense
        elseif inputObject.Delta.y > 0 then
            angleY = angleY + (1 + inputObject.Delta.y/msmooth) * msense
        end
    end
end)    

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

                if angleY > zoomMax then
                    angleY = zoomMax
                elseif angleY < -zoomMax then
                    angleY = -zoomMax
                end

                local x = 5 * math.cos(math.rad(angleX))
                local z = 5 * math.sin(math.rad(angleX))
                local y = 2.5 * math.tan(math.rad(angleY))

                local camPosition = Vector3.new(x, y, z)
                camPosition = camPosition + plr.Torso.Position  

                cam.CoordinateFrame = CFrame.new(camPosition, plr.Torso.Position)*CFrame.new(2,2,3)         
    end)
end)    

--run--

game.Players.LocalPlayer.Character.Humanoid.Died:connect(function()
    cam.CameraType = Enum.CameraType.Custom
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic
end)

script.Parent.Settings.TextButton.MouseButton1Click:connect(function()

msense = math.floor(script.Parent.Settings.msense.CanvasPosition.X/112.5)
msmooth = math.floor(script.Parent.Settings.smooth.CanvasPosition.X/31.5)
zoomMax = 80

camCnt()

for i,c in pairs (script.Parent.Settings:GetChildren()) do
    c.Visible = false
end

end)

Update:

I got some code that sort-of works, but is still inefficient, I would love if I got a better math equation for this.

--Third person camera stuff--

repeat wait() until game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name)
--Set up--
ip = game:GetService("UserInputService")
cam = game.Workspace.CurrentCamera
plr = game.Players.LocalPlayer.Character
plr.Humanoid.AutoRotate = false
m = game.Players.LocalPlayer:GetMouse()

--This would be an example of forced settings, though I made a GUI settings example.
--[[
msense = 10 --Might want to make a user controllable value. I personally prefer 10x but I'm also a very fast paced gamer.
msmooth = 35 --How much is the mouse.Delta divided by?
zoomMax = 80 --The highest the camera can me rotated on the y axis, so you can't glitch it. 85 is exavtly 180 degrees.
]]--

--Coroutines--

camCnt = coroutine.wrap(function()
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CoordinateFrame = CFrame.new(plr.Torso.Position+Vector3.new(2,2,3))
    ip.MouseBehavior = Enum.MouseBehavior.LockCenter
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson   

    local angleX = 0
    local angleY = 0

ip.InputChanged:connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
        if inputObject.Delta.x < 0 then
            angleX = angleX - (1 - inputObject.Delta.x/msmooth) * msense
        elseif inputObject.Delta.x > 0 then
            angleX = angleX + (1 + inputObject.Delta.x/msmooth) * msense
        end
        if inputObject.Delta.y < 0 then
            angleY = angleY - (1 - inputObject.Delta.y/msmooth) * msense
        elseif inputObject.Delta.y > 0 then
            angleY = angleY + (1 + inputObject.Delta.y/msmooth) * msense
        end
    end
end)    

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

                if angleY > zoomMax then
                    angleY = zoomMax
                elseif angleY < -zoomMax then
                    angleY = -zoomMax
                end

                local dir = Vector3.new(m.hit.p.x,0,m.hit.p.z)
                local x = 5 * math.cos(math.rad(angleX))
                local z = 5 * math.sin(math.rad(angleX))
                local y = 2.5 * math.tan(math.rad(angleY))      

                local camPosition = Vector3.new(x, y, z)
                camPosition = camPosition + plr.Torso.Position  

                cam.CoordinateFrame = CFrame.new(camPosition, plr.Torso.Position)*CFrame.new(2,2,3) 
                plr.Torso.CFrame = CFrame.new(plr.Torso.CFrame.p,dir)       
    end)
end)    

--run--

game.Players.LocalPlayer.Character.Humanoid.Died:connect(function()
    cam.CameraType = Enum.CameraType.Custom
    game.Players.LocalPlayer.CameraMode = Enum.CameraMode.Classic
end)

script.Parent.Settings.TextButton.MouseButton1Click:connect(function()

msense = math.floor(script.Parent.Settings.msense.CanvasPosition.X/112.5)
msmooth = math.floor(script.Parent.Settings.smooth.CanvasPosition.X/31.5)
zoomMax = 80

camCnt()

for i,c in pairs (script.Parent.Settings:GetChildren()) do
    c.Visible = false
end

end)
0
I'm too tired to look at the equations! Merp. I'll take a look to see if I can come up with a better equation. deputychicken 226 — 8y
0
Alright, sounds cool. drahsid5 250 — 8y
0
Shift lock marine5575 359 — 3y

Answer this question