I’m trying to make an event system to my game where meteors will fall from the sky. The script chooses a random position within the bounds of the map, and then uses raycasting to (1 detect if a part (platform) is beneath that random position, and (2 use Ray.Position to correct the positioning. However, the Y value of this Ray.Position is always the same, and it’s always above the detected part.
Here’s the code:
repeat
chosenPosition = Vector3.new(math.random(mapBounds.X.Lower,mapBounds.X.Upper), mapBounds.Y.Upper, math.random(mapBounds.Z.Lower,mapBounds.Z.Upper))
ray = workspace:Raycast(chosenPosition, (-(mapBounds.Y.Upper - mapBounds.Y.Lower)-200)*Vector3.yAxis, params)
if ray and ray.Instance and (ray.Instance.Transparency ~= 0 and ray.Instance.CanCollide == true) and ray.Position then
chosenPosition = ray.Position
else
continue
end
for i,v in pairs(usedPositions) do
if (v - chosenPosition).Magnitude <= 20 then
acceptable = false
end
end
wait()
until acceptable == true
table.insert(usedPositions, chosenPosition)
local referencePart = Instance.new("Part", folder)
referencePart.Anchored = true
referencePart.CanCollide = false
referencePart.Transparency = 1
referencePart.Position = chosenPosition
meteor.Position = Vector3.new(chosenPosition.X+direction*(((mapBounds.Y.Upper+100) - chosenPosition.Y)/250)*100,mapBounds.Y.Upper+100,chosenPosition.Z+direction*(((mapBounds.Y.Upper+100) - chosenPosition.Y)/250)*100)
local attachment0 = Instance.new("Attachment", meteor)
attachment0.Name = "MeteorAttachment0"
local attachment1 = Instance.new("Attachment", referencePart)
attachment1.Name = "MeteorAttachment1"
lv = (attachment0.WorldPosition - attachment1.WorldPosition).Unit
attachment1.Orientation = Vector3.new(lv.X*lv.Z, lv.X*lv.Y,lv.Y*lv.Z)
attachment1.WorldPosition = meteor.Position
local alignPosition = Instance.new("AlignPosition", meteor)
alignPosition.RigidityEnabled = true
alignPosition.Attachment0 = attachment0
alignPosition.Attachment1 = attachment1
local alignOrientation = Instance.new("AlignOrientation", meteor)
alignOrientation.RigidityEnabled = true
alignOrientation.Attachment0 = attachment0
alignOrientation.Attachment1 = attachment1
meteor.Parent = folder
meteor.Anchored = false
local tween = tweenService:Create(attachment1, TweenInfo.new(2.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {Position = Vector3.zero})
tween:Play()