Okay, so lets say, I have a sphere thats colliding with a cube:
Example
Top view
When the spherecollides 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
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).
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.
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:
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.
(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.
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:
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)