How to cast rays outwards of an object and use this to add velocity to another object?

Okay, so lets say, I have a sphere thats colliding with a cube:

Example

Top view
1

When the sphere collides with/touches something (exept a player) I want to add velocity to that cube, so that it goes outwards of the sphere (like a explosion):

Example

Top view
2

With the help of others, I figured, that this would work by raycasting. Now I don’t know how to cast rays outwards of the sphere where the cube collides with the sphere and I don’t know how to add velocity to the cube in that direction (with it also being affected by gravity, just like bullet drop).

Example

Side view
3

1 Like

Hello
I really don’t understand what you want achive
I would be happy when you more write it in more details
Thanks.

I want to cast rays in the direction of where one part collides with the other. Then I want to give that part a velocity along the ray. So basically its giving a part knockback from touching another part.

You basically take the direction portion of the ray (which is the second parameter). Then get the .Unit of that and multiply it by the speed you want it to go at, which you can then apply as the velocity of the part.

Mhm I’m understand, I think the knockback will be automatic with ROBLOX physics.

I think he’s trying to create a custom explosion, which means he’ll have to add that functionality himself.

You wouldn’t need rays for this - you’d just need the positions of both balls at the time of the collision. If you take the unit of the (cube’s position - sphere’s position), you can get the direction of the cube from the sphere, then you multiply it by a specific speed and apply the velocity:

cube.Velocity = (cube.Position - sphere.Position).Unit * 100

If you finding way to make custom explosion then there is already topic

As I know velocity in part does not working
He should create a BodyVelocity and set to what you have said

pretty sure BodyVelocity is legacy, so you shouldn’t use that.
Also, the .Velocity property of a part does work for me. But if it doesn’t, you should try VectorForce instead.

Legacy doesn’t mean you can’t use them, you should only not use them if its deprecated

legacy means it’s recommended that you don’t use them, because there are newer/better options.

So I tried your method with

cube.Velocity = (cube.Position - sphere.Position).Unit * 100

and I wrote this function:

image
(note: “quack” is just a folder I put into parts when I don’t want them affected by it)

but now this happens when I the sphere hits a part + it ignores that my character has a humanoid inside of it and goes complete wild which instantly teleports me into the void.

image

You shouldn’t post screenshots of code, rather use a codeblock. Also, you should show the entire code pertaining to the objective because I do not know where you obtain your hit and sphere variables, and you’re updating it’s Position, when you’re supposed to update the Velocity

I fixed it now but there’s still an error message:

image

LocalScript which is inside a tool
local player = game.Players.LocalPlayer
local tool = script.Parent
local RepStor = game:GetService("ReplicatedStorage")
local velocityEvent = RepStor:WaitForChild("velocityEvent")


tool.Activated:Connect(function()
	
if not player.Character then
    print("waiting for character")
    player.CharacterAdded:wait();
	    char = player.Character
	else
	    char = player.Character
	end

local hand = char:FindFirstChild("RightHand")

sphere = Instance.new("Part")
sphere.Parent = hand
sphere.Shape = "Ball"
sphere.Name = "CheckSphere"
sphere.CanCollide = false
sphere.Transparency = 1
sphere.Position = hand.Position

sphere.Touched:Connect(function(hit)
	
		if not hit:FindFirstChild("quack") then
			velocityEvent:FireServer(hit)
		end

		
	end)

	
	wait()
	
	sphere:Destroy()
end)
Script called velocityEvent
local RepStor = game:GetService("ReplicatedStorage")
local unanchorEvent = RepStor:WaitForChild("velocityEvent")

unanchorEvent.OnServerEvent:Connect(function(player, hit)
	
	local parts = player.leaderstats.Parts
			
			if not player.Character then
   						print("waiting for character")
    					player.CharacterAdded:wait();
					    char = player.Character
					else
					    char = player.Character
					end
					
					hand = char.RightHand
					sphere = hand:FindFirstChild("CheckSphere")

					hit.Velocity = (hit.Position - hand.Position).Unit * 100
			
			
			for i, instance in pairs(hit:GetDescendants()) do

					if instance:IsA("Weld") and not hit.Parent:FindFirstChild("Humanoid") and not hit.Parent.Parent:FindFirstChild("Humanoid") and not hit.Parent.Parent.Parent:FindFirstChild("Humanoid") then
						instance:Destroy()
						parts.Value = parts.Value + 1
					end
					
					if not hit:FindFirstChild("Humanoid") or not hit:FindFirstChild("quack") then
						hit.Velocity = (hit.Position - sphere.Position).Unit * 100
					end
					
			end
		end)

Ok I fixed it

30characters