Crater generates few studs away from where i'm standing

Been struggling with this one for a while now, this crater spawns a few studs away from me;
So depending on where i am on my map, the crater get’s positioned differently:

	local RootPosition = things.Origin.Position
	
	local craterSize = 10
	local radius = 5
	local rnd = Random.new()
	local numCircles = rnd:NextInteger(3, 5)

	for h = 1, numCircles do

		local numParts = rnd:NextInteger(10, 15)

		for i = 1, numParts do

			local rock = Instance.new("Part")

			local rockSize = Vector3.new(rnd:NextNumber(1.5, 2.2), rnd:NextNumber(1, 2), rnd:NextNumber(1.4, 2))
			rockSize *= h/numCircles

			local orientation = Vector3.new(rnd:NextNumber(-60, 60), rnd:NextNumber(-60, 60), rnd:NextNumber(-60, 60))

			local xOffset = rnd:NextNumber(-radius, radius)
			local zOffset = rnd:NextNumber(-radius, radius)
			local lookAt = Vector3.new(RootPosition.X + xOffset, RootPosition.Y, RootPosition.Z + zOffset)

			local LookVector = CFrame.new(RootPosition, lookAt).LookVector
			local position = RootPosition + (LookVector * radius * math.clamp(h/numCircles, 0.4, 1))
			local NewCFrame = CFrame.new(position, RootPosition + LookVector + orientation)

			local RayParams = RaycastParams.new()
			RayParams.FilterDescendantsInstances = {workspace.Map}
			RayParams.FilterType = Enum.RaycastFilterType.Include
			
			local floorRay = workspace:Raycast(NewCFrame.Position + Vector3.new(0, 3, 0), position - Vector3.new(0, 100, 0), RayParams)

			if floorRay and floorRay.Instance.Anchored then

				local colour = floorRay.Instance.Color
				local mat = floorRay.Instance.Material

				rock.Size = Vector3.new(0, 0, 0)
				rock.Position = floorRay.Position
				rock.Orientation = orientation

				rock.CanCollide = false
				rock.Anchored = true
				rock.TopSurface = Enum.SurfaceType.Smooth
				rock.Color = colour or Color3.fromRGB(83, 86, 90)
				rock.Material = mat or Enum.Material.Slate

				rock.Parent = workspace.Effects
				
				TweenModule.obj(rock, "Quad", "Out", .3, {Size = rockSize})

				task.spawn(function()

					task.wait(1)
					
					local fallTween = TweenModule.obj(rock, "Quad", "Out", .3, {Size = Vector3.new(0,0,0)})
					fallTween.Completed:Wait()

					rock:Destroy()
				end)
			end
		end

		task.wait()
	end
2 Likes

bump
sadhahfiudahsiufhbaisudiuasd

2 Likes

i’ve been struggling for way too much with this script; im bumping again

1 Like

What is the things?

Did you want it around the HumanoidRootPart instead?

2 Likes

yes, things is just a table of info im passing through, in this case just HumanoidRootPart

1 Like

Perhaps just set the HRP Directly to see if it changes anything.

1 Like

I tried that already, i tried all sorts of things, i need to change the code somehow

1 Like

I may be completely wrong here but I can’t find any documentation in the 2-3 mins I looked. You may want to look for longer. With LookVector I don’t really understand how it exactly works but I’m thinking it tells the computer to cast a ray (of sorts) from the cframe to the front surface of the object.

You have used LookVector for the position and since the position is most likely the point of the ray on the front surface of the HRP perhaps it’s using that point as the centre of your circle. I can’t think of anything else.

2 Likes

Your code for placing the rocks can be simplified considerably, but as-is, it’s going to put the circles of rocks at the right location, whatever the X and Z values of RootPosition are will be the center of the crater. So if something’s wrong, it’s in the code that sets things.Origin, and the RootPosition is not the same position as the character’s root part when the code runs. So the offset problem is not in the bit code you’ve included, it’s somewhere else in your code.

1 Like