How would I match texture offset with character movement so texture appears stationary

I am trying to make an animated ocean. I am doing this by moving a large part with the player wherever they go. This allows me to add an animated texture to give the appearance that the water is moving.

Although when the character is stationary, the water animation looks perfect, when the character starts moving it hinders this effect. This is because as the part moves with the player, so does the texture on top of it making it look very strange.

See video:

what im trying to achieve:

To counteract this I know I need to offset the texture depending on where the character has moved to but that is where I am struggling, would anyone have any ideas?

This is my code so far:


local newWaterTexture = waterTexture:Clone()
newWaterTexture.Parent = workspace
local textureOffset = newWaterTexture.TextureOffset

local runService = game:GetService("RunService")

local floorPos = 52

local foreOffsetU = Instance.new("NumberValue")
local foreOffsetV = Instance.new("NumberValue")

local foreTexture = newWaterTexture:WaitForChild("Texture")
local backTexture = newWaterTexture:WaitForChild("BackTexture")


newWaterTexture.Position = Vector3.new(rootPart.Position.X,floorPos,rootPart.Position.Z)
local previousPos = newWaterTexture.Position

runService.RenderStepped:Connect(function()
	newWaterTexture.Position = Vector3.new(rootPart.Position.X,floorPos,rootPart.Position.Z)
	
	--trying to calculate offset due to movement here
	local offsetX = previousPos.X - rootPart.Position.X
	local offsetZ = previousPos.Z - rootPart.Position.Z
	
	
	previousPos = newWaterTexture.Position
	
	foreTexture.OffsetStudsU = foreOffsetU.Value - offsetX
	foreTexture.OffsetStudsV = foreOffsetV.Value - offsetZ
end)


local foreTween1 = tweenService:Create(foreOffsetU,tweenInfo1,{Value = 50})
local foreTween2 = tweenService:Create(foreOffsetV,tweenInfo1,{Value = 50})
local foreTween3 = tweenService:Create(foreOffsetU,tweenInfo1,{Value = 0})
local foreTween4 = tweenService:Create(foreOffsetV,tweenInfo1,{Value = 0})



local backTween1 = tweenService:Create(backTexture,tweenInfo1,{OffsetStudsU = -50})
local backTween2 = tweenService:Create(backTexture,tweenInfo1,{OffsetStudsV = -50})
local backTween3 = tweenService:Create(backTexture,tweenInfo1,{OffsetStudsU = 0})
local backTween4 = tweenService:Create(backTexture,tweenInfo1,{OffsetStudsV = 0})

--playing the tweens below

fixed, just had to stop updating the previous position

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