Im redesigning a shielding system and Im having trouble doing the correct math:

I have a script move a ball in the middle of two other parts. this is a good start but as the two parts separate, the part remains in the center.
I want to keep the ball close to the green part, but also have it align with the red part:

what should I do to accomplish this? here is my current script:
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(1,1,1)
part.Parent = game.Workspace
part.Anchored = true
while true do
wait()
local mag = script.Parent.Part.Position + (script.Parent.Part2.Position - script.Parent.Part.Position)/2
part.Position = mag
end
1 Like
Ah you could do ball.Position = greenPart.Position + (greenPart.Position - redPart.Position).Unit * distanceFromGreenPart.
Now the (greenPart.Position - redPart.Position).Unit is basically getting the direction from the green part to the red part and then the distanceFromGreenPart is how far you want the ball to be in studs.
As of writing I’m a bit tired but if you’ve got any questions feel free to ask and I hope this helps ya mate
4 Likes
I had to make a few changes, but I managed to get it working, thanks for pointing me in the right direction, also if your curious about the working script here it is:
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(1,1,1)
part.Parent = game.Workspace
part.Anchored = true
while true do
wait()
part.Position = script.Parent.Part.Position + (script.Parent.Part.Position - script.Parent.Part2.Position).Unit * -10
end
1 Like
I’m glad I could be of some help, also keep that .unit in mind as its very useful for alot of things
I also just realised that it only gives you the local direction I should of added that one on but at least you got it mate.