I’m trying to make a game where you click on a character, and then click on a grid square to move it there.
I’m using bodypositions at the moment to move the characters (as they are not humanoids) but the problem is, the bodypositions move at the same speed regardless of how far the target is.
I’ve tried looking for solutions but I can’t seem to find any anywhere. I tried using Tweens and using Pythagoras theorum to calculate the distance between the character and the assigned grid square, but for some reason it erased the character from existence.
You were on the right track for using the Pythagoras theorem, but you probably did a small mistake somewhere causing your character to disappear.
As speed is equal to distance divided by time, and you need to find the time, we can just move the equation around:
speed = distance / time speed * time = distance time = distance / speed
Therefore, in order to find the time, we just need to divide the distance by the speed desired.
local TS = game:GetService("TweenService")
local character: Model = workspace.Character
local startPosition = character:GetPrimaryPartCFrame()
local endPosition = Vector3.new(10, 0, 10)
local desiredSpeed = 10
-- time = distance / speed
local distance = (startPosition - endPosition).Magnitude
local requiredTime = distance / desiredSpeed
local info = TweenInfo.new(requiredTime, Enum.EasingStyle.Linear) -- fill out the rest if you'd like
local tween = TS:Create(character, info, {CFrame = endPosition})
tween:Play()