I’m trying to make a part move in a random direction whilst being confined within a given space but I just cant get it. Can someone help me?
first, calculate the center point and the size of the space where you want to confine movement. to ensure the random values stay within the space, generate a random value between 0 and the size of the space (size.X
, size.Y
, size.Z
). then, subtract half the size (size.X / 2
) to center the random value, allowing it to be either positive or negative within the confined area.
you can then use tweens, or another method, to move your object to this random position while keeping it within the space.
here is what that would look like
local TweenService = game:GetService("TweenService")
local mover = workspace:FindFirstChild("mover")
local confinedSpace = workspace:FindFirstChild("confinement")
local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Back)
-- untility functions if you don't want to use a tween
local function getDirection(pointA, pointB)
return (pointB - pointA).Unit
end
local function getDistance(pointA, pointB)
return (pointB - pointA).Magnitude
end
local function lerp(start, target, lerpFactor)
return start + (target - start) * lerpFactor
end
-- main function to retrieve a random position within a vector3
function getRandomVector3(targetPos, targSize)
-- get midpoint of confinement area and generate random X, Y, Z values within size bounds
local position = targetPos
local size = targSize
local randomX = math.random() * size.X - size.X / 2
local randomY = math.random() * size.Y - size.Y / 2
local randomZ = math.random() * size.Z - size.Z / 2
-- return random position within the confines of the space
return Vector3.new(position.X + randomX, position.Y + randomY, position.Z + randomZ)
end
while true do
local randomPos = getRandomVector3(confinedSpace.Position, confinedSpace.Size)
local tween = TweenService:Create(mover, tweenInfo, {Position = randomPos})
tween:Play()
tween.Completed:Wait()
task.wait(2)
end
The part is supposed to move smoothly across terrain but also should be spinning.
I don’t think a tween can do that can it? I’ve tried and maybe i just did it wrong.
if i were you, i’d perform a raycast downwards to see if the random position isn’t somewhere inside some terrain. but, for the spinning portion, you should use a hinge constraint with its actuator type set to motor, its max acceleration set to a high value (i.e. 250,000), and its max torque set to infinite.
can you even do that with a tornado cause thats when im working on. yeah…
(the tornado is attached to an invisible part that is being tweened, it is spinning with hinge constraints)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.