My model moves underground when tweening

Helloo. Theres an issue where my model is hovering over the ground and tweening under the floor.



Here is my script


local me = script.Parent
local positions = me.Parent.Positions:GetChildren()
local animator = script.Parent.AnimationController.Animator
local body = me["Sphere.003"]
local TS = game:GetService("TweenService")
local walktime




while true do
	wait(math.random(1, 10))
	--d=√((x2 – x1)² + (y2 – y1)²).
	local randoposition = positions[math.random(1, #positions)]
	local x1, y1 = body.Position.X, body.Position.Z
	local x2, y2 = randoposition.Position.X, randoposition.Position.Z
	local part1 = x2 - x1
	part1 = part1 ^ 2
	local part2 = y2 - y1
	part2 = part2 ^ 2 
	local distance = math.sqrt(part1 + part2)
	print(distance)
	walktime = distance / 10
	
	local Info = TweenInfo.new(
		walktime,
		Enum.EasingStyle.Linear
	)
	TS:Create(body, Info, {CFrame = randoposition.CFrame}):Play()



end

(Also if you can help me make it so the model faces the part its moving to that would be swag)

1 Like

The model moves underground because you’re moving it’s center of mass to the part’s CFrame.

You should add half of the mesh’s height to the model’s pivot/mesh’s CFrame in order to mitigate this.

Also please please PLEASE don’t use math.sqrt() to calculate distance. Use Vector3.Magnitude like so:

local distance = (part1.Position - part2.Position).Magnitude

It’ll help you out a LOT in the long run.
You can also make the bug face the part by using CFrame.lookat(), since a CFrame is the combination of a Vector3 and a part’s orientation.

1 Like

Holey moleeyyy i didnt even know that existed, sorry!!

1 Like

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