How would I get the direction a part is moving in by vector?

Hello. I am trying to make a sonic boom effect which includes a sound effect and a cone that moves outwards from the player position. How would I make this cone move away from the player from the opposite direction they’re moving towards?

image
visualized**

By the way, here is the current code I have (only the code for setting the cone cframe)

function repeatfunction(f,args,t) --repeats the given function every heartbeat for t seconds
	local current = tick()
	local i = 0
	repeat
		i += 1
		f(i)
		rs.Heartbeat:Wait()
	until math.abs(tick()-current)>=t
	
	return
end
	repeatfunction(function(i)
			local hrp = char:WaitForChild("HumanoidRootPart")
			local cf = hrp.CFrame
			local pos = cf.Position
			
			--part.CFrame = CFrame.lookAt(pos+(cf.LookVector*-(15+(i*2))),pos)*CFrame.Angles(math.rad(-90),0,0)
			part.CFrame = CFrame.lookAt(pos-(hrp.Velocity),pos)*CFrame.Angles(0,math.rad(180),0)
		end,nil,2)

I already have all the code in for detecting the players speed and all of that. All I need is to find a way to get a vector for the direction the player is moving in. Like CFrame.LookVector. I tried velocity, but if the player moves fast, then the cone position will be super far away.

Use the .Unit attribute of Vector3s. It will shorten the vector such that its magnitude is one.

print(Vector3.new(100, 200, 0).Unit) --> 0.4472135901451111, 0.8944271802902222, 0
2 Likes

btw im pretty sure this can be simplified to:

part.CFrame = CFrame.lookAt(pos-hrp.Velocity, pos-hrp.Velocity*2)

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