Tweening to a moving object

How do I tween this orb to a moving object? I want to leave the EasingStyle as it is.

orb.Touched:Connect(function(hit)
	if hit.Name == "PickupHitbox" then
		TweenService:Create(
			orb,
			TweenInfo.new(
				.5,
				Enum.EasingStyle.Back,
				Enum.EasingDirection.In,
				0,
				false,
				0
			),
				
			{Position = hit.Position}
		):Play()	
	end
end)
1 Like

I understand what you want to achieve.

But when Orb has reached its goal, do you want it to stop or continue endlessly?

1 Like

I would probably do orb:Destroy()
And btw “hit” is the player’s character

1 Like

The most proffesional solution to make an Orb act like something like Minecraft XP is to use Interpolation. :Lerp().

However it will appear as a Linear tween, and you will lose your tween style.

2 Likes

As far as I know Lerp won’t alow me to use Enum.EasingStyle.Back

1 Like

Okay try this.

local TweenService = game:GetService("TweenService")

orb.Touched:Connect(function(hit)
	if hit.Name == "PickupHitbox" then
		repeat
			task.wait(0.01)
			local distance = (orb.Position - hit.Position).Magnitude
			local tween = TweenService:Create(
				orb,
				TweenInfo.new(
					0.5,
					Enum.EasingStyle.Back,
					Enum.EasingDirection.In,
					0,
					false,
					0
				),
				{Position = hit.Position}
			)
			tween:Play()
		until distance < 1
        orb:Destroy()
	end
end)

wont do much because it always applies Enum.EasingStyle.Back and duration is constant, so it will never end and be slow

edit: also the ball will constantly move backwards, because of the easingstyle

1 Like

That is true, I would instead use Roblox forces then.
Moving the part with a force towards the player,
depending on your settings ofcourse the orb can overshoot and miss the player,
but still wanting to go towards the player. Giving you that Back EasingStyle but in a way
cooler way if you ask me.

1 Like

I’ve generated this code with ChatGPT,
and you should definitely ask ChatGPT for this simple task in generating a working code for the force stuff.

Best of luck!

local part = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local target = character:WaitForChild("HumanoidRootPart")

local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(4000, 4000, 4000)  -- Adjust for stronger or weaker force
bodyVelocity.Velocity = (target.Position - part.Position).Unit * 50  -- Initial force
bodyVelocity.Parent = part

-- Simulate the overshoot and return:
while (part.Position - target.Position).Magnitude > 1 do
    bodyVelocity.Velocity = (target.Position - part.Position).Unit * 50
    wait(0.05)
end

bodyVelocity:Destroy()
1 Like

bro don’t chat gpt your answers on forum, i did use chatgpt and gemini ai before posting here and couldnt find anything.
also i dont want to involve ANY physics

1 Like

Use an easing function.

This code was generated by chatgpt:

  • t : elapsed time
  • b : beginning value (Orb.Position.X)
  • c : change in value (end value minus start value) e.g. Object.Position.X - Orb.Position.X
  • d : duration (how long your easing takes)
function BackEaseIn(t, b, c, d, s)
    s = s or 1.70158 -- The overshoot amount, can be adjusted for more/less "back" effect
    t = t / d
    return c * t * t * ((s + 1) * t - s) + b
end

Here is the code if all that was too confusing: (i wrote this myself)

local function BackEaseIn(t, b, c, d, s)
    s = s or 1.70158 -- The overshoot amount, can be adjusted for more/less "back" effect
    t = t / d
    return c * t * t * ((s + 1) * t - s) + b
end

local Target = Object
local Orb = Orb

local Duration = 0.5; -- in seconds

local StartTime = tick()

repeat
	Orb.Position = Vector3.new(
		BackEaseIn(tick() - StartTime, Orb.Position.X, Target.Position.X - Orb.Position.X, Duration),
		BackEaseIn(tick() - StartTime, Orb.Position.Y, Target.Position.Y - Orb.Position.Y, Duration),
		BackEaseIn(tick() - StartTime, Orb.Position.Z, Target.Position.Z - Orb.Position.Z, Duration)
	)

	task.wait(1 / 60)
until tick() - StartTime >= Duration

Orb.Position = Object.Position

Make sure to say your issues here
Its 3 am for me right now and there are 3 cups of coffee in my digestive system (been awake for 18 hours)
So expect issues in this code :sob:

1 Like

Nope, the idea to use forces was not generated with AI - only the code as mentioned,
I simply generated a code for you on, to give you an idea of how to implement forces.

But thanks for letting me know that you do not want to use forces,
but I will continue to use ChatGPT 4o for my workflow.

Best of luck!

1 Like

May I get thy precious solved tag since using your own easing function is currently the only way of tweening into a moving object :​D

1 Like

im currently testing and debugging your code, if it solves my issue, i will add your precious tag haha

2 Likes

after debugging your code, this is the final and working version:

orb.Touched:Connect(function(hit)
	if hit.Name == "PickupHitbox" then
		local function easeInBack(t, b, c, d, s)
			s = s or 1 -- The overshoot amount, can be adjusted for more/less "back" effect
			t = t / d
			return c * t * t * ((s + 1) * t - s) + b
		end

		local Duration = 0.5;

		local StartTime = tick()

		repeat
			orb.Position = Vector3.new(
				easeInBack(tick() - StartTime, orb.Position.X, hit.Position.X - orb.Position.X, Duration),
				easeInBack(tick() - StartTime, orb.Position.Y, hit.Position.Y - orb.Position.Y, Duration),
				easeInBack(tick() - StartTime, orb.Position.Z, hit.Position.Z - orb.Position.Z, Duration)
			)

			task.wait(1 / 60)
		until orb.Position == hit.Position
	end
end)

Thanks for helping!

2 Likes

Great to hear it worked!
Also you may want to remove the 1 / 60 in task.wait if you want your easing to look even smoother in 120 fps. That’s a mistake I made.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.