Tweening an inverse CFrame

I want to make these NPCs rotate towards my avatar. They need to be welded to the bus stop, so I need to tween the weld C0. So far my code is this: (where “thing” is my avatar’s rootpart)


but for some reason, the NPCs seem to be going to 0,y,0 and then back when tweening it.
https://gyazo.com/f6dd92830be3b8395d0364da654722a7
I suspect it has something to do with :Inverse(). Any ideas?

1 Like

It looks like when its tweening the cframe the cframe’s position is being set to the origin or someplace far off. Try removing the inverse and see what the difference is.

Also why are they welded? Do they ever become unwelded and move around or get on a bus?
Because you could probably have easier cframe tweening if they are just anchored. You would not need to do the C0, but just the HumanoidRootPart’s CFrame.

1 Like

That shouldn’t work because of how weld.C0 works, and I’ve just tried it and it’s worsened the issue. The C0 that it’s setting to is correct as if I set it without tweening it, it works. But when I tween it, that effect happens for some reason.

They need to be welded as eventually I’ll need to weld them to the bus so they can move while inside it

1 Like

If I just set the CFrame to the lookAt code, it works as normal. The issue is using tween to interpolate between them. I’ve tried lerp and it’s done the same thing.

1 Like

If I multiply the weld.C0 by a cframe.angles, this bouncing effect still happens…

1 Like

https://gyazo.com/a0d1b03ea886172c8f7ba08a8ab2e130 Tweening C0 with no change in rotation

https://gyazo.com/7a894e94be007aabdec54e838d256a7f Tweening C0 with a change in rotation

1 Like

Ok, I did a test place where I try to get the same results.
It has 2 ways of doing it, one is by anchored npc’s the other is by welded npc’s I still think the anchored are better.
RotateNpcsToPlayer.rbxl (100.1 KB)


The npc’s are in a folder, with a controlling script for each.
image
The Welded npc’s have a weld in HumanoidRootPart, and the C0 is by default just a CFrame.new()

Here is the code for the ANCHORED npc’s

local dir = script.Parent
local base = dir:WaitForChild("Base")

local speed = .2
local tweenInfo = TweenInfo.new(
	speed,
	Enum.EasingStyle.Linear
)

while true do
	--Find closest player 
	local target = nil
	for _,i in pairs(game.Players:GetPlayers()) do
		if i.Character then
			if target == nil then
				target = i.Character
			else
				if (i.PrimaryPart.Position-base.Position).Magnitude < (target.PrimaryPart.Position-base.Position).Magnitude then
					target = i.Character
				end 
			end
		end
	end
	
	if target then
		for _,i in pairs(dir:GetChildren()) do
			if i.Name == "Npc" then
				local targetCFrame = CFrame.lookAt(i.PrimaryPart.Position,target.PrimaryPart.Position)
				local tween = game.TweenService:Create(i.PrimaryPart,tweenInfo,{CFrame = targetCFrame})			
				tween:Play()
			end
		end
	end
	wait()
end

Here is the code for the WELDED npc’s

local dir = script.Parent
local base = dir:WaitForChild("Base")

local speed = .2
local tweenInfo = TweenInfo.new(
	speed,
	Enum.EasingStyle.Linear
)

while true do
	--Find closest player 
	local target = nil
	for _,i in pairs(game.Players:GetPlayers()) do
		if i.Character then
			if target == nil then
				target = i.Character
			else
				if (i.PrimaryPart.Position-base.Position).Magnitude < (target.PrimaryPart.Position-base.Position).Magnitude then
					target = i.Character
				end 
			end
		end
	end
	
	if target then
		for _,i in pairs(dir:GetChildren()) do
			if i.Name == "Npc" then
				local weld = i.PrimaryPart.Weld
				
				local rotationCFrame = CFrame.lookAt(i.PrimaryPart.Position,target.PrimaryPart.Position)
				rotationCFrame = rotationCFrame - rotationCFrame.Position
				
				local objectCFrame = rotationCFrame:Inverse()
				local tween = game.TweenService:Create(weld,tweenInfo,{C0 = objectCFrame})		
				tween:Play()
			end
		end
	end
	wait()
end

Not totally sure how you are doing your code, based on just the little ‘facesomething’ function you shared, but maybe this well help you in some way.

2 Likes

I very much appreciate this, I wasn’t expecting somebody to try for themselves. I guess it improves your own coding skills. I’ll have a look now, thanks.

1 Like

Never mind, I’ve got it. Part1.CFrame:Inverse()*Part0.CFrame works perfectly. Thanks a lot for that place file, it’s definitely put me in the right direction.

Sorry I didn’t get back to you sooner, but glad you figured it out.
Good luck with your project.

1 Like

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