Ray.Position always same Y value

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()
1 Like

jesus can u at least break this line down, it’s literally impossible to understand and that probably explains why you’re having issues with it

1 Like

And yes it looks like the y here is mapBounds.Y.Upper+100 so it would be the same every time.

That’s not the issue: that’s where the meteor spawns; if you follow the script, it then follows attachment1 which is being tweened. Read the script!

I’ve managed to solve my issue: accidentally left the acceptable variable as true so when the conditions didn’t meet it left it at the randomized chosen position which had a constant Y value.

1 Like

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