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

How do I prevent this script from making my character move side to side?

Asked by
2_MMZ 1059 Moderation Voter
2 years ago
Edited 2 years ago

I recently started using this script and when using it at first, I detected no issues. But, when I started to use it for my horror games, I noticed that when holding a gear, it sways side to side when I don't want it to. So I made a script that makes my character visible, and I can see that the character is moving left to right while going forward. Is there any way that I can prevent this?

Video: https://gyazo.com/59516d689c869a79da3716d559e72a1d

Script:

local runService = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local chr = plr.Character
local humrootpart = chr:WaitForChild("HumanoidRootPart")
local hum = chr:WaitForChild("Humanoid")
local cam = workspace.Camera

local tiltSpeedZ = 0.1
local bobbingSpeed = 0.1

local tilt = 0
local sinValue = 0

function lerp(a, b, t)
    return a + (b - a) * t
end

function calculateSine(speed, intensity)
    sinValue += speed 
    if sinValue > (math.pi * 2) then sinValue = 0 end
    local sineY = intensity * math.sin(2 * sinValue)
    local sineX = intensity * math.sin(sinValue)
    local sineCFrame = CFrame.new(sineX, sineY, 0)
    return sineCFrame
end

local previousSineX = 0
local previousSineY = 0
runService.RenderStepped:Connect(function(dt)
    local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
    local speedModifier = (hum.WalkSpeed / 16)
    tilt = math.clamp(lerp(tilt, movementVector.X * tiltSpeedZ, 0.1), -0.25, 0.1) 

    local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
    local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0.1)
    local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0.1)

    cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
    previousSineX = lerpedSineX
    previousSineY = lerpedSineY
end)
0
From what i can see this script is used to make a camera Bobbling effect and the player is not actually moving side to side also this may not be related but how did you make that flashlight Puppynniko 1059 — 2y
0
When making the character visible, the character moves side to side even on other players screens. Also, the flashlight is a gear that is equipped when the player presses F. 2_MMZ 1059 — 2y
0
are you talking about where the flashlight is moving around? Puppynniko 1059 — 2y
0
oh now i see the problem this may be caused by the script moving the camera left and right  when going first person the player moves where you look at and the game thinks your moving your camera left and right thats why it breaks Puppynniko 1059 — 2y

1 answer

Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
2 years ago
Edited 2 years ago

Try this this works but im not really sure what im doing lol

local runService = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local chr = plr.Character
local humrootpart = chr:WaitForChild("HumanoidRootPart")
local hum = chr:WaitForChild("Humanoid")
local cam = workspace.CurrentCamera

local tiltSpeedZ = 0.1
local bobbingSpeed = 0.1
local TiltMultiplier = 2
local tilt = 0
local sinValue = 0

function lerp(a, b, t)
    return a + (b - a) * t
end

function calculateSine(speed, intensity)
    sinValue += speed 
    if sinValue > (math.pi * 2) then sinValue = 0 end
    local sineY = intensity * math.sin(2 * sinValue)
    local sineX = intensity * math.sin(sinValue)
    local sineCFrame = CFrame.new(sineX, sineY, 0)
    return sineCFrame
end
local previousSineX = 0
local previousSineY = 0

runService.RenderStepped:Connect(function(dt)
    local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
    local speedModifier = (hum.WalkSpeed / 16)
    local T = tick()
    tilt = math.clamp(lerp(tilt, movementVector.X * tiltSpeedZ, 0.1), -0.25, 0.1) 

    local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
    local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0.1)
    local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0.1)
    local Bobble = Vector3.new(lerpedSineX, lerpedSineY, 0)
    cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.Angles(0,0,math.rad(lerpedSineY*TiltMultiplier))
    --hum.CameraOffset = hum.CameraOffset:lerp(Bobble, 0.25)
    --cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
    workspace.Part.CFrame = CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
    --[[if hum.MoveDirection.Magnitude > 0 then
        cam.CFrame *= CFrame.Angles(0,0,math.rad(distance * math.sin(tick() * speed)))
    end]]
    previousSineX = lerpedSineX
    previousSineY = lerpedSineY
end)

or this, this is the Rotation

local distance = 4 --the distance it ends up waving
local speed = 8 --how fast it is
local runservice = game:GetService("RunService")
local Player = game:GetService("Players")
local LocalPlayer = Player.LocalPlayer
local cam = workspace.CurrentCamera
local Humanoid = LocalPlayer.Character:WaitForChild("Humanoid")

runservice.RenderStepped:Connect(function()
    if Humanoid.MoveDirection.Magnitude > 0 then
        cam.CFrame *= CFrame.Angles(0,0,math.rad(distance * math.sin(tick() * speed)))
    end
end)
0
I made a new view bobbing script myself, but thanks! 2_MMZ 1059 — 2y
Ad

Answer this question