Problem with tweening

Im trying to make a simple camera move forward script and right now its working but its moving sideways not forward, how could I fix this?


local camera = workspace.CurrentCamera
local partA = workspace.Cam.A
local partB = workspace.Cam.B


local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local partB = workspace.Cam.A


local speed = 5 
local distance = (partB.Position - humanoidRootPart.Position).Magnitude
local duration = distance / speed


local forwardDirection = (partB.Position * humanoidRootPart.Position).Unit


local targetPosition = humanoidRootPart.Position + (forwardDirection * distance)


local tween = game:GetService("TweenService"):Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Linear), {CFrame = CFrame.new(targetPosition)})
local playerGui = script.Parent
local greenButton = playerGui:WaitForChild("Green"):WaitForChild("Start")

greenButton.Activated:Connect(function()
	script.Parent.Green:Destroy()
	script.Parent.Background.Enabled = true
	while true do 
		wait(.50)
		script.Parent.Background.Frame.BackgroundTransparency += 0.1
	end
	greenButton:Destroy()
	tween:Destroy()
end)
tween:Play()


tween.Completed:Wait()


tween:Destroy()

image

3 Likes

I’m not good at scripting but here’s what I think:
The issue that can be causing this sideways movement instead of the forward movement is how is the forwardDirection variable and how it’s being used. Instead of using multiplication, you should use subtraction to calculate the direction vector between partB.Position and humanoidRootPart.Position. Also, if you want the camera to move foward regardless of which way the camera is facing, you can use the CFrame.lookAt method and extract the LookVector.

1 Like

local camera = workspace.CurrentCamera
local partA = workspace.Cam.A
local partB = workspace.Cam.B


local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local partB = workspace.Cam.A


local speed = 5 
local distance = (partB.Position - humanoidRootPart.Position).Magnitude
local duration = distance / speed


local forwardDirection = (partB.Position - humanoidRootPart.Position).Unit


local targetPosition = humanoidRootPart.Position - (forwardDirection * distance)


local tween = game:GetService("TweenService"):Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Linear), {CFrame = CFrame.new(targetPosition)})
local playerGui = script.Parent
local greenButton = playerGui:WaitForChild("Green"):WaitForChild("Start")

greenButton.Activated:Connect(function()
	script.Parent.Green:Destroy()
	script.Parent.Background.Enabled = true
	while true do 
		wait(.50)
		script.Parent.Background.Frame.BackgroundTransparency += 0.1
	end
	greenButton:Destroy()
	tween:Destroy()
end)
tween:Play()


tween.Completed:Wait()


tween:Destroy()

would something like this work

1 Like

Setting the cframe as 0, 0, -1 is the same as “forward”. And then multiply by the distance

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local localPlayer = Players.LocalPlayer
local camera = workspace.CurrentCamera
local partB = workspace.Cam.B

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local speed = 5 
local distance = (partB.Position - humanoidRootPart.Position).Magnitude

local duration = distance / speed
local cameraTweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear)
local targetCFrame = humanoidRootPart.CFrame * CFrame.new(0, 0, -1 * distance)

local tween = TweenService:Create(camera, cameraTweenInfo, {CFrame = targetCFrame})
local playerGui = script.Parent
local greenButton = playerGui:WaitForChild("Green"):WaitForChild("Start")

greenButton.Activated:Connect(function()
	script.Parent.Green:Destroy()
	script.Parent.Background.Enabled = true
	while true do 
		wait(.50)
		script.Parent.Background.Frame.BackgroundTransparency += 0.1
	end
	greenButton:Destroy()
	tween:Destroy()
end)

tween:Play()
tween.Completed:Wait()
tween:Destroy()

In the video, the moving red bricks front face is the green line, and that would be your camera in this case. Then the green brick is the destination.

2 Likes

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