Fly script makes my char go to some center point of workspace and then work

  1. What do you want to achieve? Keep it simple and clear!
    Topic name is kinda complicated, so here is vid
  2. What is the issue? Include screenshots / videos if possible!
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? I defenitely have no point why this happens

Code:

local flying = false
local speed = 0.5

local rs = game:GetService("RunService")
local Char = script.Parent.Parent
local root = Char:WaitForChild("HumanoidRootPart")
local camera = game.Workspace.CurrentCamera
local Fly = workspace.RemoteEvents.Console.Functions.Fly
local UIS = game:GetService("UserInputService")


function fly()
	local bp = Instance.new("BodyPosition")
	bp.MaxForce = Vector3.new()
	bp.D = 10
	bp.P = 10000
	bp.Parent = root
	local bg = Instance.new("BodyGyro")
	bg.MaxTorque = Vector3.new()
	bg.D = 10
	bp.MaxForce = Vector3.new(400000,400000,400000)
	bg.MaxTorque = Vector3.new(400000,400000,400000)
	bg.Parent = root
	UIS.InputBegan:Connect(function(input, gameProcessed)
		while UIS:IsKeyDown(Enum.KeyCode.W) and Char.Configs.CanFly.Value == true do
		rs.RenderStepped:Wait()
		bp.Position = root.Position +((root.Position - camera.CFrame.p).unit * speed)
		bg.CFrame = CFrame.new(camera.CFrame.p, root.Position)
	end
	end)
end

function endFlying()
	root.BodyPosition:Destroy()
	root.BodyGyro:Destroy()
end

Fly.OnClientEvent:Connect(function(toggle)
	if toggle == "enable" then
		fly()
	else
		endFlying()
	end
end)
1 Like

You can set the inital position of the bodyposition to the root part position

1 Like

Well that works, but now when i execute fly, my character rotates directly to 90* , also when i stop pressing W camera shakes a bit

Same as body position, set the bodygyro inital cframe to the rootpart.CFrame.Rotation value which is a CFrame value with rotation only.

Try using .Stepped instead of render stepped since it happens before physics and is recommended for making physics changes.

Yeah it fixes rotation to 90*, but camera still shakes even with .stepped

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