Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trajectory of a snowball not working correctly?

I'm trying to use the equation that describes the trajectory of a projectile for a snowball in my game. I think I put the equation correctly but it's not working.

Server Script:

eventsFolder.FireSnowball.OnServerEvent:Connect(function(player,angle,mousePos,playerPos,vectorPos,velocity,height,vector,playerCFrame)
	
	local breakBool = false
	local i = 1
	local snowball = game.ServerStorage.Snowball.Handle:Clone()
	snowball.Parent = game.Workspace
	snowball.Position = playerPos
	snowball.Name = "Snowball"
	local part = Instance.new("Part") --Creates a part at the vector position so lerp works
	part.Parent = game.Workspace
	part.Transparency = 1
	part.CanCollide = false
	part.Anchored = true
	part.Position = vectorPos
	
	while true do
		
		snowball.Touched:Connect(function(hit)
			if hit.Parent ~= player and hit.Parent ~= game.Workspace then
				--Destroys the snowball and breaks the loop
				print(hit.Parent)
				snowball:Destroy()
				breakBool = true
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 20
				end
			end
		end)
		
		if breakBool == true then
			break
		end
		
		local cos = math.cos(angle)
		local xAxis = (velocity/vector.Magnitude)*i
		local lerp = playerCFrame:Lerp(part.CFrame,xAxis)
		local x, y, z, m11, m12, m13, m21, m22, m23, m31, m32, m33 = lerp:components()
		local newCF = CFrame.new(x, height + y + xAxis*math.tan(angle)-(9.8*(xAxis*xAxis)/(2*(velocity*cos)*(velocity*cos))), z, m11, m12, m13, m21, m22, m23, m31, m32, m33)
		snowball.CFrame = var2
		i = i+1
		wait(0.03)
	end
	
	part:Destroy()
end)

Localscript(this is working):

local button = script.Parent
local event = game.ReplicatedStorage.RemoteEvents.ToggleSnowball
local event2 = game.ReplicatedStorage.RemoteEvents.FireSnowball
local velocity = 3
local border = button.Parent.Outline
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local debounce = true

button.MouseButton1Down:Connect(function(player)
	
	if not character or not character.Parent then
    	character = player.CharacterAdded:wait()
	end
	
	if border.Visible == false then
		
		border.Visible = true
		
		for i,gui in pairs(button.Parent.Parent:GetChildren()) do
			if gui:FindFirstChild("Outline") then
				if gui.Name ~= button.Parent.Name then
					gui.Outline.Visible = false
				end
			end
		end
		
		event:FireServer(player)		
		
		while border.Visible == true do
			
			mouse.Button1Down:Connect(function()
				if debounce == true then
					debounce = false
					local mousePos = mouse.Hit.p
					local mouseVector = mousePos - character.HumanoidRootPart.Position
					local parallelVector = Vector3.new(mousePos.X,character.HumanoidRootPart.Position.Y,mousePos.Z) - character.HumanoidRootPart.Position
					local angle = math.deg(math.acos(mouseVector:Dot(parallelVector)/(mouseVector.Magnitude * parallelVector.Magnitude) ) )
					local height = character.HumanoidRootPart.Position.Y
					event2:FireServer(angle,mousePos,character.HumanoidRootPart.Position,Vector3.new(mousePos.X,character.HumanoidRootPart.Position.Y,mousePos.Z),velocity,height,parallelVector + character.HumanoidRootPart.Position,character.HumanoidRootPart.CFrame)
					wait(0.1)
					debounce = true
				end
			end)
			
			wait(0.1)
	
		end
		
		event:FireServer(player)
	else
		border.Visible = false	
	end
	
end)

The snowball just ends up behaving the same way no matter what angle I throw it at.

Also, here is the equation for the trajectory of a projectile: y = h + x * tan(?) - g * x² / 2 * V?² * cos²(?)

Thanks! (Also sorry for the lack of comments)

1 comment

0

I think it could be that the xAxis variable is always a fraction, but when I make a new variable for it with a value of just velocity*i the snowball just disappears instantly

1 answer

I came back to this after nearly a year, and I found a much simpler solution to my problem. I was trying to make a snowball shoot at different velocities depending on the camera angle.

If you're someone who's looking to do this in the future, don't do what I did and pull out calculus to bash the velocity. Just take the camera lookVector and multiply it by a velocity.

This discussion is preserved as it was. New answers, comments, and votes are closed.