How To Deflect Part Off Wall

I made a script when a player touches a part it flys away from them. I am wanting to know how to add a feature to my script, when the part hits another part it will bounce back or off the part.

I have an idea of how to do it but I don’t know how to do that and if that is the best way to do it.

How do I accomplish this?

local TweenService = game:GetService("TweenService")

local function onTouch(hit)
	local character = hit.Parent
	local tool = character:FindFirstChildOfClass("Tool")

	if tool and tool:FindFirstChild("Handle") then
		local direction = character:GetPrimaryPartCFrame().lookVector
		local distance = 10 -- Adjust the distance as needed
		local duration = 1 -- Adjust the duration of the tween as needed

		local endPosition = part.Position + direction * distance

		local tweenInfo = TweenInfo.new(duration)
		local goal = {}
		goal.Position = endPosition

		local tween = TweenService:Create(part, tweenInfo, goal)
		tween:Play()
	end
end

part.Touched:Connect(onTouch)
3 Likes

You want it to bounce or deflect? A simple bounce effect can be simulated with the tweenservice by using the Bounce easing style, however deflecting will cause you to do some more stuff.

I think i want it to deflect. When the part hits a wall i want it to go of the wall. As if you throw a ball at the wall in real life

Maybe this post will help:

I found some what a way to deflect but it doesn’t deflect the ball it deflects the object. I don’t know how to flip it where the ball get fling in stead. How do I do that?

local part = script.Parent -- Assuming the script is a child of the part you want to move
local TweenService = game:GetService("TweenService")

local function onTouch(hit)
	local character = hit.Parent
	local tool = character:FindFirstChildOfClass("Tool")
	local direction = (part.Position - hit.Position).unit
	local flingForce = 500 -- Adjust the force as needed

	if character:FindFirstChildOfClass("Part") then
		part.Velocity = direction * flingForce

	elseif tool and tool:FindFirstChild("Handle") then
		local direction = character:GetPrimaryPartCFrame().lookVector
		local distance = 50 -- Adjust the distance as needed
		local duration = 1 -- Adjust the duration of the tween as needed

		local endPosition = part.Position + direction * distance

		local tweenInfo = TweenInfo.new(duration)
		local goal = {}
		goal.Position = endPosition

		local tween = TweenService:Create(part, tweenInfo, goal)
		tween:Play()
	end
end

part.Touched:Connect(onTouch)

I placed a part in workspace then put this script in it:

local part = script.Parent -- Assuming the script is a child of the part you want to move
local TweenService = game:GetService("TweenService")
local canTouch = true



local function onTouch(hit)
	
	if hit.Name ~= "Baseplate" then

		if canTouch == true then
			canTouch = false

			local character = hit.Parent
			local tool = character:FindFirstChildOfClass("Tool")
			local direction = (part.Position - hit.Position).unit
			local flingForce = 40 -- Adjust the force as needed

			if character:FindFirstChildOfClass("Part") then
				part.Velocity = direction * flingForce

			elseif tool and tool:FindFirstChild("Handle") then
				local direction = character:GetPrimaryPartCFrame().lookVector
				local distance = 50 -- Adjust the distance as needed
				local duration = 1 -- Adjust the duration of the tween as needed

				local endPosition = part.Position + direction * distance

				local tweenInfo = TweenInfo.new(duration)
				local goal = {}
				goal.Position = endPosition

				local tween = TweenService:Create(part, tweenInfo, goal)
				tween:Play()
			end
			canTouch = true
		end

	end
end

part.Touched:Connect(onTouch)

I added a few parts to workspace for it to bounce off. It seems to work failry well.

1 Like

The problem is that the part is not moving but it make the player deflect of the part.

the second part of the script looks for the player if they have a tool and when they touch the part it goes in the direction of they are facing. This is what makes the part move in the beginning. I have it this way so when the part hits of the walls it will keep moving.Nothing is happening to the part and hit just sets there.

Is your “part” anchored?

I noticed it will move the player instead if it is.

The part is anchored, because the part is hovering above the ground and i want it to stay around the same y cordednets

Ok after looking for a while I found this How can I reflect projectiles off walls?
In this there is a video on exactly what I want to happen or post.

I already have a part that I want for the object and after looking at the scripts I don’t know how to remove the makings of the fireball and also comining my script and the fireball scripts to gether. How do I do this?

Also in the post it has a script, a local script, and a remotevent. Is there also a way to do this with out all of that and just make it in one script, or is it better to do it that way?