Move object 10 studs

Hi, I will try to explain this as good as I can but, I found a formula to find the direction of what the players humanoid root part is looking at, so I used that formula then I tried to change it by changing the position to be 10 studs farther. I tried many things and couldn’t find a single solution, my brain is not good with CFrame.

This script spawns rocks at the root parts position

function StraightRocks(root,player)
	local parms = RaycastParams.new()
	parms.FilterDescendantsInstances = {workspace.Ability.Debris,characters}
	parms.FilterType = Enum.RaycastFilterType.Blacklist
	local angle = 0
	-- Creating Rocks
	for i = 1,5 do
		local size = math.random(4,7)
		local height = math.random(8,15)
		local part = Instance.new("Part")
		local sound = Instance.new("Sound")
		sound.Parent = part
		sound.SoundId = "rbxassetid://9125869138"
		sound.RollOffMaxDistance = 200
		sound.RollOffMinDistance = 6
		sound.Volume = 1.2
		local direction = player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector * player.Character.HumanoidRootPart.Position.Magnitude
		-- Properties
		part.Anchored = true
		part.Size = Vector3.new(1,1,1)
		part.CFrame = CFrame.lookAt(player.Character.HumanoidRootPart.Position, direction)
		game.Debris:AddItem(part,5)
		local raycast = workspace:Raycast(part.CFrame.p,part.CFrame.UpVector * -10, parms)
		if raycast then
			part.Position = raycast.Position + Vector3.new(0,-5,0)
			part.CanCollide = false
			part.Material = Enum.Material.Rock
			part.Color = raycast.Instance.Color
			part.Size = Vector3.new(8,height,8)
			part.Orientation = Vector3.new(part.Orientation.X,math.random(-180,180),part.Orientation.Z)
			part.Parent = workspace.Ability.Debris
			sound:Play()
			local Tween = game:GetService("TweenService"):Create(part,TweenInfo.new(.25,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = part.Position + Vector3.new(0,5,0)}):Play()
			delay(4,function()
				local Tween = game:GetService("TweenService"):Create(part,TweenInfo.new(1),{Transparency = 1}):Play()		
			end)
		end
		angle += 100
		wait(0.2)
	end
end

i gotchu

function StraightRocks(root,player)
	local parms = RaycastParams.new()
	parms.FilterDescendantsInstances = {workspace.Ability.Debris, characters}
	parms.FilterType = Enum.RaycastFilterType.Blacklist
	local angle = 0
	-- Creating Rocks
	for i = 1,5 do
		local size = math.random(4,7)
		local height = math.random(8,15)
		local part = Instance.new("Part")
		local sound = Instance.new("Sound")
		sound.Parent = part
		sound.SoundId = "rbxassetid://9125869138"
		sound.RollOffMaxDistance = 200
		sound.RollOffMinDistance = 6
		sound.Volume = 1.2
		local lv = player.Character.HumanoidRootPart.CFrame.LookVector
		local direction = player.Character.HumanoidRootPart.Position + (lv * ((i+1) * (3 + i)))
		-- Properties
		part.Anchored = true
		part.Size = Vector3.new(1,1,1)
		part.CFrame = player.Character.HumanoidRootPart.CFrame
		game.Debris:AddItem(part,5)
		local raycast = workspace:Raycast(part.CFrame.p,part.CFrame.UpVector * -10, parms)
		if raycast then
			part.Position = raycast.Position + Vector3.new(0,-5,0)
			part.CanCollide = false
			part.Material = Enum.Material.Rock
			part.Color = raycast.Instance.Color
			part.Size = Vector3.new(8,height,8)
			part.Orientation = Vector3.new(part.Orientation.X,math.random(-180,180),part.Orientation.Z)
			part.Parent = workspace.Ability.Debris
			sound:Play()
			local Tween = game:GetService("TweenService"):Create(part,TweenInfo.new(.25,Enum.EasingStyle.Bounce,Enum.EasingDirection.Out),{Position = direction}):Play()
			delay(4,function()
				local Tween = game:GetService("TweenService"):Create(part,TweenInfo.new(1),{Transparency = 1}):Play()		
			end)
		end
		angle += 100
		wait(0.2)
	end
end
1 Like

I begun working on a solution before this post was marked as solved, so I’m still going to shared what I came up with. Here’s what my solution looks like:

This solution is different because it actually spawns the rocks where they should spawn instead of underneath the player at the beginning. It also spawns them all at the same location, because that’s what the original script did. Here’s what this script looks like:

local NUM_ROCKS = 5
local ROCK_FADE_TIME = 1
local ROCK_LIFE_TIME = NumberRange.new(4, 5)
local ROCK_HEIGHT_RANGE = NumberRange.new(2, 4)
local ROCK_OFFSET_FROM_CHARACTER = Vector3.new(0, 0, -10)

local TweenService = game:GetService("TweenService")

local characterFolder = workspace:FindFirstChild("Characters")
local abilityFolder = workspace:FindFirstChild("Ability")

local debrisFolder = abilityFolder:FindFirstChild("Debris")

function straightRocks(player)
	local character = player.Character
	if character==nil or character.PrimaryPart==nil then
		return
	end
	
	local floorRaycastParams = RaycastParams.new()
	floorRaycastParams.FilterDescendantsInstances = {debrisFolder, characterFolder}
	floorRaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local inFrontOfCharacter = character:GetPivot() * CFrame.new(ROCK_OFFSET_FROM_CHARACTER)
	local floorRaycastResult = workspace:Raycast(inFrontOfCharacter.Position, Vector3.yAxis*-10, floorRaycastParams)
	if floorRaycastResult==nil then
		return
	end
	
	for i = 1,NUM_ROCKS do	
		local rockRandom = Random.new(i)
		local rockHeight = rockRandom:NextNumber() * (ROCK_HEIGHT_RANGE.Max - ROCK_HEIGHT_RANGE.Min) + ROCK_HEIGHT_RANGE.Min
		local rockAngle = rockRandom:NextInteger(1, 360)
		local rockLifetime = rockRandom:NextNumber(ROCK_LIFE_TIME.Min, ROCK_LIFE_TIME.Max)
		
		local newRock = Instance.new("Part")
		task.delay(rockLifetime, function()
			newRock:Destroy() 
		end)
		
		newRock.CFrame = CFrame.new(floorRaycastResult.Position) * CFrame.new(0, -rockHeight/2, 0) * CFrame.Angles(0, math.rad(rockAngle), 0)
		newRock.Size = Vector3.new(8, rockHeight, 8)
		newRock.Material = Enum.Material.Rock
		newRock.Color = floorRaycastResult.Instance.Color
		newRock.Anchored = true
		newRock.CanCollide = false
		newRock.CanTouch = false
		newRock.CanQuery = false
		newRock.Parent = debrisFolder
		
		local newRockSound = Instance.new("Sound")
		newRockSound.SoundId = "rbxassetid://9125869138"
		newRockSound.RollOffMaxDistance = 200
		newRockSound.RollOffMinDistance = 6
		newRockSound.Volume = 1.2
		newRockSound.Parent = newRock
		newRockSound:Play()
		
		TweenService:Create(newRock, TweenInfo.new(.25, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {CFrame = newRock.CFrame * CFrame.new(0, rockHeight, 0)}):Play()
		
		task.delay(rockLifetime-ROCK_FADE_TIME, function()
			TweenService:Create(newRock, TweenInfo.new(ROCK_FADE_TIME), {Transparency = 1}):Play()
		end)
		
		task.wait(.2)
	end
end

As you can see, there are a few constants at the top of the script that should hopefully be self-explanatory. Instead of providing both the player and the root part to the function, all you need to provide is the player. When getting the pivot point of a character with :GetPivot(), it should be the root part’s CFrame since it is the primary part of the model, which affects the pivot point. To get 10 studs in front of the player, all you need is the CFrame of the root part and an offset:

local inFrontOfCharacter = character:GetPivot() * CFrame.new(Vector3.new(0, 0, -10))

The line above is probably the most important take away from this solution.

2 Likes

Your solution was neat but them spawning in the same place was not intentional :sweat_smile:
image

The original post doesn’t mention anything about spacing the rocks out, but does mention moving the rocks forward ten studs.