How would I go about unanchored smoothed flying part?

Include a standalone, bare-bones rbxl file with only the code you want reviewed.

  • Code Review is for reviewing specific parts of your code, and not your whole game.
  • Code Review is intended for improving already-working code. If you need help debugging your code, please use Scripting Support.

Provide an overview of:

  • What does the code do and what are you not satisfied with? This is for a Bee that will randomly fly and have states like sleeping, Idle, and flying
  • What potential improvements have you considered? It gets abit wack because its unanchored but I need it to be unanchored.
  • How (specifically) do you want to improve the code? How would I make it go wack when running. Anyways to help make unanchor parts float smoothly?
-- Server Script for Bee Behavior Management

-- Define possible states for the bee
local states = {
	Flying = "Flying",
	Sleeping = "Sleeping",
	Idle = "Idle"
}

-- Initialize current state to Flying
local currentState = states.Flying

-- Reference to the AlignOrientation instance
local alignOrientation = script.Parent:FindFirstChild("AlignOrientation")

-- Function to check if the part is close enough to the ground
local function isNearGround(part)
	local rayOrigin = part.Position
	local rayDirection = Vector3.new(0, -50, 0) -- Adjust distance as needed
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

	return raycastResult and (raycastResult.Distance < 5) -- Adjust threshold as needed
end

-- Function to generate random movement vectors
local function getRandomMovementVector()
	local minSpeed = 5 -- Minimum speed for the part
	local maxSpeed = 20 -- Maximum speed for the part
	local angle = math.rad(math.random(0, 360)) -- Random angle for movement
	local speed = math.random(minSpeed, maxSpeed) -- Random speed within range
	local dx = speed * math.sin(angle)
	local dz = speed * math.cos(angle)
	return Vector3.new(dx, 0, dz)
end

-- Function to handle the Flying state
local function flyState(part)
	if isNearGround(part) then
		-- If the part is near the ground, adjust its Y position slightly above the ground
		part.CFrame = CFrame.new(part.Position.X, part.Position.Y + 1, part.Position.Z)
	else
		-- Generate a random movement vector and apply it to the part
		local movementVector = getRandomMovementVector()
		part.Velocity = movementVector

		-- Update Attachment0 to face the direction of movement
		local attachment0 = part:FindFirstChild("Attachment0")
		if attachment0 then
			local forwardDirection = movementVector.Unit * 10 -- Scale factor to control how far away the attachment appears
			attachment0.CFrame = CFrame.new(part.Position) * CFrame.lookAt(Vector3.new(0, 0, 0), forwardDirection)
		end
	end
end



-- Function to handle the Sleeping state
local function sleepState(part)
	-- Simply stop the part from moving
	part.Velocity = Vector3.new(0, 0, 0)

	-- Optionally reset AlignOrientation if needed
	if alignOrientation then
		alignOrientation.CFrame = CFrame.identity
	end
end

-- Function to handle the Idle state
local function idleState(part)
	if isNearGround(part) then
		-- If the part is near the ground, adjust its Y position slightly above the ground
		part.CFrame = CFrame.new(part.Position.X, part.Position.Y + 1, part.Position.Z)

		-- Optionally reset AlignOrientation if needed
		if alignOrientation then
			alignOrientation.CFrame = CFrame.identity
		end
	end
end

-- Main loop for the part
while wait() do
	local part = script.Parent -- Assuming the script is a child of the part

	-- Switch states randomly every 30 seconds
	if os.time() % 30 == 0 then
		local keys = {}
		for k in pairs(states) do
			table.insert(keys, k)
		end
		currentState = states[keys[math.random(#keys)]]
	end

	if currentState == states.Flying then
		flyState(part)
	elseif currentState == states.Sleeping then
		sleepState(part)
	elseif currentState == states.Idle then
		idleState(part)
	end
end

1 Like