( SOLVED ) How to force a Vector2 Orientation to face towards a Vector3?

I’m working on a “pull” mechanic for my camera system, where the camera gets pulled towards a position and the player can fight back for control. Currently my camera CFrame is set depending on X/Y values dictated by the mouse. Pulling the mouse right would change the MouseYValue to 90, etc.

How can change my X/Y values to point towards a world position?

local function mouseMovement(_, inputState, inputObject)
		if not isMouseLocked and not isRMBDown then return end
		local delta = Vector2.new(inputObject.Delta.X / Config.CAMERA_WEIGHT, inputObject.Delta.Y / Config.CAMERA_WEIGHT) * Config.CAMERA_SMOOTH
		local X_EST = mouseXAngle - delta.Y
		mouseXAngle = (X_EST >= Config.CAMERA_ANGLE_LIMITS.Max and Config.CAMERA_ANGLE_LIMITS.Max) or (X_EST <= Config.CAMERA_ANGLE_LIMITS.Min and Config.CAMERA_ANGLE_LIMITS.Min) or X_EST
		mouseYAngle = (mouseYAngle - delta.X) % 360

function CameraController:PointCameraAtPosition(position)
	for i = 1, 500 do
		mouseXAngle = lerp(mouseXAngle, 0, Config.CAMERA_WEIGHT)
		mouseYAngle = lerp(mouseYAngle, 0, Config.CAMERA_WEIGHT)

Mouse doesn’t have a depth so likely you’re going to want to look at ViewportPointToRay to construct a unit ray given an X and Y coordinate. Be mindful that it is a ray.

The inverse would be WorldToViewportPoint. Couldn’t tell since the title says “convert world position to mouse XY” and the thread says “convert mouse XY to world position”. Two inverse operations ended up being part of your explanation so might as well give both methods.

1 Like

I’m not sure how to use ViewportPointToRay in this situation, i apologize for the incorrect title, what i’m attempting to do is force a Vector2 to face towards a Vector3 on the X and Y axis’.

This is a certified X/Y problem.

I’ve figured out my issue however I’ve run into the Gimbal Lock issue, so i’ll be creating a new post.
Here’s my code if anyone somehow has a similar issue.

local function lerp(start, goal, alpha)
	return start + (goal - start) * alpha

local targetCFrame = CFrame.new(CAMERA.CFrame.Position, position)
local newRot = Vector3.new(targetCFrame:ToOrientation()) / math.pi * 180
mouseXAngle = lerp(mouseXAngle, newRot.X, 0.05)
mouseYAngle = lerp(mouseYAngle, newRot.Y, 0.05)

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