Making the character look towards the mouse during a tween

What steps would I need to take in order to make it so I can still freelook around during the tween,

Currently the tween gets the current position of the player and tweens it up by 15 studs,
However I have another function which makes the character look towards the mouse.hit, During the tween the character will no longer look at the mouse.hit, I suspect this is because of the way I’m getting the CFrameValue but I’m unsure how I can correct it, any ideas?

function GraviticFlux.activateUltimate(inputObj, isTyping)
	if inputObj.KeyCode == Enum.KeyCode.X and not debounce then
		debounce = true
		rootPart.Anchored = true
		
		GraviticFlux.LookAtMouse()
		GraviticFlux.Tween(character, rootPart.CFrame * CFrame.new(0,15,0))
		
		task.wait(5)
		connection:Disconnect()
		rootPart.Anchored = false
	end
end

function GraviticFlux.LookAtMouse()
	connection = RunService.RenderStepped:Connect(function(dt)
		local rootPos, mousePos = rootPart.Position, mouse.Hit.Position
		rootPart.CFrame = CFrame.new(rootPos, Vector3.new(mousePos.X, mousePos.Y, mousePos.Z))
		
	end)
end

function GraviticFlux.Tween(model, CF)
	local CFrameValue = Instance.new("CFrameValue")
	CFrameValue.Value = model:GetPrimaryPartCFrame()
	
	CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
		model:SetPrimaryPartCFrame(CFrameValue.Value)
	end)
	
	local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	
	local tween = TweenService:Create(CFrameValue, tweenInfo, {Value = CF})
	tween:Play()

	tween.Completed:Connect(function()
		CFrameValue:Destroy()
	end)
end

1 Like

The easiest way would probably be to add the look at mouse part to the cframevalue changed.
Also a quick tip for the cframevalue, .Changed always returns the value instead of property on value instances. I would also suggest using PivotTo and GetPivot instead of primary part since it’s depercated. Try doing something like this

CFrameValue.Changed:Connect(function(newValue)
	model:PivotTo(newValue * CFrame.Angles(CFrame.new(rootPosition, mousePosition):ToOrientation()))
end)

the reason the look at mouse stops working during the tween is because the render loop runs first, then the tween, and since the tween changes the cframe which includes orientation, it tkind of cancels out the render loop

2 Likes

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