I am trying to create an effect using textures and the player camera which moves the texture in the direction that the camera is moving.
However, I found that when attempting to do so with a part that doesn’t have an aligned axis with the camera, the texture doesn’t move in the same direction. (Having a rotation property in textures to determine the direction that OffsetStudsU/V moves in would be amazing.)
I was able to somewhat fix this problem with by trying to translate the camera position to be in line with the texture’s offset axes, but when the orientation of the part the texture is applied to is ~120 degrees, or at all negative, the solution I tried to apply completely breaks.
https://streamable.com/6bm9qv (Latter half of the recording displays the problem)
The code I am using:
local Camera = workspace.Camera
local function Get_Offset(Origin, Magnitude, Theta)
local X, Y do
X = Origin.X + (Magnitude * math.cos(Theta))
Y = Origin.Y + (Magnitude * math.sin(Theta))
end
return X, Y
end
local function Follow_Cam(PrimaryPart)
local _, Rot = PrimaryPart.CFrame:ToEulerAngles()
local Difference = (Vector2.new(PrimaryPart.Position.X, PrimaryPart.Position.Z) - Vector2.new(Camera.CFrame.Position.X, Camera.CFrame.Position.Z))
local U, V = Get_Offset(
Vector2.new(PrimaryPart.Position.X, PrimaryPart.Position.Z),
Difference.Magnitude,
(Rot - (math.atan2(Difference.Y, Difference.X) - math.pi) * -1)
)
PrimaryPart.Texture.OffsetStudsU = U
PrimaryPart.Texture.OffsetStudsV = V
end