I’m trying to make a minigame where a green ball needs to avoid the red balls. You can move the green ball with basic controls. The red balls spawn in on any of the 4 skulls. The red ball then tweens to move to the position of the green ball. This works as intended, but once the red ball has reached the tween position it stops. How would I achieve the red ball continuing to move past the green ball?
Make a while-loop that tweens to the green ball.
I meant for the red ball to move in the same direction it was moving, once it reaches the green ball
Well I guess you could get the direction by subtracting its target position by the starting position and normalize it.
Could you give me an example of what you mean?
direction = (green_ball_position - red_ball_starting_position)
I didn’t normalize it but idk
You could also show the code and I’ll have some ideas to implement it. So you just want the red ball to move to the edges after it moved to the green part?
local function tweeningVirus(virus)
local ball = computerGui.Background.FirewallMinigame.Ball
local movePosition = UDim2.new(ball.Position.X.Scale + 0.5, 0, ball.Position.Y.Scale + 0.5, 0)
local tween = game:GetService("TweenService"):Create(virus, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {Position = movePosition})
tween:Play()
tween.Completed:Wait()
wait(5)
for i = 1, 10 do
virus.BackgroundTransparency += 0.1
wait()
end
virus:Destroy()
end
local waitTime = 1.5
for i = 1, number do
local virus = computerGui.Background.FirewallMinigame.Virus:Clone()
virus.Parent = computerGui.Background.FirewallMinigame
virus.Position = virusSpawnpoints[math.random(1, #virusSpawnpoints)]
virus.Visible = true
computerGui.Background.FirewallMinigame.VirusShoot:Play()
task.spawn(tweeningVirus, virus)
if waitTime > 0.5 then
waitTime -= 0.1
end
wait(waitTime)
end
This is pretty simple but it requires some math i’m suprised no one has responded yet.
Let me go through the steps to achieve this :
The idea is to get the equation of the line that passes through the 2 points and find the coordinates of wherever we want the ball to go within that line.
First we have 2 points, Let’s call them A,B. What we need to do first is calculate the equation of the line that passes them to do that you can check How to Find the Equation of a Line from Two Points.
After we find the equation which would be something like y = mx+b, we now can find the coordinates of whatever point we want within that line, all we have to do is pick a x value for example and we can determine y. If we implement this into code we have:
local Xa,Ya = virus.AbsolutePosition.X, virus.AbsolutePosition.Y
local Xb, Yb = ball.AbsolutePosition.X , ball.AbsolutePosition.Y
local m = (Yb-Ya) / (Xb-Xa)
local b = Yb - m * Xb
local Yt = 80 -- Let's pick Y equal to 80
local Xt = (Yt - b) / m -- Xt (target X within that line if we decide that Y is equal to 80)
local tween = t:Create(virus,TweenInfo.new(5),{Position = UDim2.fromOffset(Xt,Yt)})
tween:Play()
IMPORTANT you must have virus parented to the ScreenGui
directly not to any frame so that the Offset position would be the same as the absolute position
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.