Physics script help (baseball system)

Hello, i need to do something like that β†’ when the ball touches the red box I want it to be pushed (the ball) like the red line that i marked



How can I do that? Does anyone know? :frowning:

1 Like

Try giving it a certain BodyVelocity in the opposite direction once it touches the red part.

1 Like

i don’t know how to do it with my code :frowning:

script

local tool = script.Parent
tool.RemoteEvent.OnServerEvent:Connect(function(player, event)
	local mouse = player:GetMouse()
	local Handle = tool.Handle
	tool.Parent = workspace
	Handle.CFrame = CFrame.new(Handle.Position, event)

	local bv = Instance.new("BodyVelocity",Handle)
	bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bv.Velocity = tool.Handle.CFrame.LookVector * 30 -- Change the number if you want :D
	local vel = tool.Handle.CFrame.LookVector
	
	wait(.2)
	
	local box = game.Workspace.BaseballField.boxTouch
	box.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Handle") then
		bv.Velocity = tool.Handle.CFrame.LookVector * -30
		local vel = tool.Handle.CFrame.LookVector
		end
	end)
	
	
	wait(.1)
	tool.Handle.BodyVelocity:Destroy()
	
	

	--- Adding the next part will result when they touch it then will like teleport to the position of the ball and like "take" it
	Handle.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			Handle.Anchored = false
			wait(.1)
			Handle.Anchored = true
			Handle.Anchored = false
		end
	end)
end)

script.Parent.Equipped:Connect(function()
	for i = 0, -4, -.5 do
		script.Parent.GripPos = Vector3.new(0,i,0)
		wait()
	end

	for i = -4,0,.5 do
		script.Parent.GripPos = Vector3.new(0,i,0)
		wait()
	end
end)

For that you would have to cast a ray from the baseball to its Look Vector and once the ray hits something , get the reflection of the ray using the formula : r = d - 2(d β‹… n)nand apply the velocity of the BodyVelocity as the reflection.

For reference you can use : How to reflect rays on hit

Hope this helps you

EDIT : Tried to make sure it works kinda similar to the way you want , got this :slight_smile: https://gyazo.com/5ba52aa07c29a98879fb56e8b558b32d

1 Like

you wanna invert the vector with d-2(dn) where d is the normal of the vector and n is the surface normal of where the ball hit. From there normalize that vector and apply its magnitude to be half of what we used to have (on touched)

You can try to use LookVector.
You can learn about LookVector here CFrame | Documentation - Roblox Creator Hub
or from this post How does LookVector work?