Character spins around like crazy during flight if colliding

I am trying to make a simple flight script, yet as I want the character to face the direction the camera does, I have to make it use one of the humanoid state types which doesn’t auto-rotate. The issue is that whenever I crash into anything, I spin around like crazy. There are many other posts related to this but I have tried all the methods I could find (such as using a bodygyro) yet it still doesn’t seem to help.

Code below (kinda messy since its a mix of my code, some random tutorial, and some devforum posts)

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera
local LastTapped, Tapped = false, false
local flyUpSpeed = 20
local flyDownSpeed = 20
local wdown = false
local f1 = false
local f2 = false
local f3 = false
local f4 = false
local speed = 0.5
local bp = nil
local bodyVel = nil
local v1 = Vector3.new(0, 0, 0)
local v2 = Vector3.new(0, 0, 0)
local v3 = Vector3.new(0, 0, 0)
local v4 = Vector3.new(0, 0, 0)

bp = Instance.new("BodyPosition",hrp)
bp.MaxForce = Vector3.new()
bp.D = 100
bp.Position = hrp.Position + Vector3.new(0,10,0)
bp.MaxForce = Vector3.new(400000,400000,400000)
bodyVel = Instance.new("BodyVelocity", hrp)
bodyVel.Name = "BodyVel"
bodyVel.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

local anim = hum:FindFirstChild("Animator") or Instance.new("Animator")
anim.Parent = hum
local forwardAnim = Instance.new("Animation")
forwardAnim.AnimationId = "rbxassetid://13432138726"
local rightAnim = Instance.new("Animation")
rightAnim.AnimationId = "rbxassetid://13432355116"
local leftAnim = Instance.new("Animation")
leftAnim.AnimationId = "rbxassetid://13432373743"
local backAnim = Instance.new("Animation")
backAnim.AnimationId = "rbxassetid://13432412550"
local forward = anim:LoadAnimation(forwardAnim)
local right = anim:LoadAnimation(rightAnim)
local left = anim:LoadAnimation(leftAnim)
local back = anim:LoadAnimation(backAnim)

hum.PlatformStand = true

uis.InputBegan:connect(function(Input, GPE)
	if GPE then return end
	if Input.KeyCode == Enum.KeyCode.W then
		f1 = true
		forward:Play()
		right:Stop(); left:Stop(); back:Stop()
		--hum:ChangeState(Enum.HumanoidStateType.PlatformStanding)
		if bp then
			bp:Destroy()
			bp = nil
		end
		while f1 and wait() do
			v1 = camera.CFrame.LookVector*speed*100
		end
		forward:Stop()
	end
	if Input.KeyCode == Enum.KeyCode.A then
		f2 = true
		left:Play()
		right:Stop(); forward:Stop(); back:Stop()
		--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
		if bp then
			bp:Destroy()
			bp = nil
		end
		while f2 and wait() do
			v2 = camera.CFrame.RightVector*speed*-100
		end
	end
	if Input.KeyCode == Enum.KeyCode.D then
		f3 = true
		right:Play()
		forward:Stop(); left:Stop(); back:Stop()
		--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
		if bp then
			bp:Destroy()
			bp = nil
		end
		while f3 and wait() do
			v3 = camera.CFrame.RightVector*speed*100
		end
	end
	if Input.KeyCode == Enum.KeyCode.S then
		f4 = true
		back:Play()
		right:Stop(); left:Stop(); forward:Stop()
		--hum:ChangeState(Enum.HumanoidStateType.FallingDown)
		if bp then
			bp:Destroy()
			bp = nil
		end
		while f4 and wait() do
			v4 = camera.CFrame.LookVector*speed*-100
		end
	end
end)

uis.InputEnded:Connect(function(Input)
	print("hi")
	if Input.KeyCode == Enum.KeyCode.W then
		f1 = false
		wait()
		v1 = Vector3.new(0, 0, 0)
		print(bp)
		if not bp and not f2 and not f3 and not f4 then
			bp = Instance.new("BodyPosition",hrp)
			bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bp.D = 100
			bp.Position = hrp.Position
		end
	elseif Input.KeyCode == Enum.KeyCode.A then
		f2 = false
		wait()
		v2 = Vector3.new(0, 0, 0)
		if not bp and not f1 and not f3 and not f4 then
			bp = Instance.new("BodyPosition",hrp)
			bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bp.D = 100
			bp.Position = hrp.Position
		end
	elseif Input.KeyCode == Enum.KeyCode.D then
		f3 = false
		wait()
		v3 = Vector3.new(0, 0, 0)
		if not bp and not f1 and not f2 and not f4 then
			bp = Instance.new("BodyPosition",hrp)
			bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bp.D = 100
			bp.Position = hrp.Position
		end
	elseif Input.KeyCode == Enum.KeyCode.S then
		f4 = false
		wait()
		v4 = Vector3.new(0, 0, 0)
		print(v4)
		if not bp and not f1 and not f2 and not f3 then
			bp = Instance.new("BodyPosition",hrp)
			bp.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bp.D = 100
			bp.Position = hrp.Position
		end
	end
end)
print("hi")
while wait() do
	bodyVel.Velocity = v1+v2+v3+v4
	hrp.CFrame = CFrame.new(hrp.CFrame.p) * (camera.CFrame - camera.CFrame.p)
	--print(camDir)
	--print(hum:GetState())
end

for reference, im trying to get something similar to the flight in this game or this game

(what happens)

(what im trying to achieve)

1 Like

try decreasing the maxforce

I think the torque is really high so it causes jittering

1 Like

I could try that, but I doubt thats the issue since its not jitterring, its rotating (so fast that it looks like its jittering)

(I dont have time today, ill try it tmrw)

High max force makes the character want to self correct with a lot of force, so it goes back and forth really fast and jitters

Its like a rope thats really tight (high max force) that always wants to go back to its original position so it vibrates fast

1 Like

(im not using bodygyro rn)
do you mean max force for the body position? that might be the case, but I do still want it to snap back instantly (ill look into it ig)

but that shouldn’t be the problem for why its jitterring the the first place, since the body position is destroyed when I am moving (and thus shouldn’t matter)

it definitly cant be the max force for the body velocity, since you can see that the goal im trying to achieve (the example) has a way higher speed

I tried decreasing the max force, but it just makes it continuosly jitter even when its not crashing into stuff

turns out the issue seems to be related to the character’s location changing, which changes the result and changes the location again or something

on further inspection it seems the issue is also related to how when my character starts shaking, the camera shakes, causing the character to shake even more

I have changed up my script after looking at some more devforum posts. It doesn’t exactly achieve the effect I want still, but it seems to be closer then previously

here is the updated script

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")

local myPlayer = game.Players.LocalPlayer
local myChar = myPlayer.Character
local myHRP = myChar:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera

local bp = Instance.new("BodyPosition", myHRP)
bp.MaxForce = Vector3.new()
bp.D = 10
bp.P = 10000

local bg = Instance.new("BodyGyro", myHRP)
bg.MaxTorque = Vector3.new()
bg.D = 10

bp.MaxForce = Vector3.new(400000,400000,400000)
bg.MaxTorque = Vector3.new(400000,400000,400000)

local speed = 0.5

local v1, v2, v3, v4 = Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0)

local function fly()
	if uis:IsKeyDown(Enum.KeyCode.W) then
		v1 = ((myHRP.Position - camera.CFrame.p).unit * speed)
	end
	if uis:IsKeyDown(Enum.KeyCode.S) then
		v2 = -((myHRP.Position - camera.CFrame.p).unit * speed)
	end
	if uis:IsKeyDown(Enum.KeyCode.A) then
		v3 = -(camera.CFrame.RightVector.Unit*speed)
	end
	if uis:IsKeyDown(Enum.KeyCode.D) then
		v4 = camera.CFrame.RightVector.Unit*speed
	end
	bp.Position = myHRP.Position + v1 + v2 + v3 + v4
	--bp.Position = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
	bg.CFrame = CFrame.new(camera.CFrame.p, myHRP.Position)
	v1, v2, v3, v4 = Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0)
end

rs.RenderStepped:Connect(function()
	fly()
end)

the current issue is that when I look straight up or down, the bodygyro seems to make the character vibrate. I can turn up the dampening, but in order to prevent all jitterring, i’d have to use a dampening so high that it takes 10 seconds for the character to turn around. (this only happens when the character is platform standing, but i kinda need it to do so or it wont turn to face up/down)

bumping this up because its still unsolved ples someone help me

still need help with this, would be nice if someone could atleast provide a good D value for the bodygyro as a workaround

please somebody help me cuz it is muffin time

seems like shift key is also a problem with this (it causes more spinning when looking directly downwards)

changing the dampening solved part of the problem, but it seems moreso that the issue was caused by the camera moving when the character moves, causing the character to move with it ;-;

Found a solution

(Used that but with a bodygyro because otherwise its still jittery)

bodygyro info

bg.CFrame = CFrame.new(myHRP.Position, myHRP.Position + Vector3.new(camera.CFrame.LookVector.X, camera.CFrame.LookVector.Y , camera.CFrame.LookVector.Z))

(dampening is 200)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.