Center a part onto a UI area

Hello! I’m making an NPC system and when the user talks to an NPC, it focuses the face of the NPC. When they open up the shop area, a pop-up shows on the right side where the user can sell items. I’d like to center the head of the NPC in the area where the pop-out is not. I’ve tried googling, but I can’t quite get a decent query so I figured I’d ask here.


Current position of the head. It is centered on the screen. I’d like the head to be centered in the red circle, which is the area where the UI is not.

Any ideas on how I could do this? I tried using some sort of proportion relating screen space to world space, but that didn’t work.

this may be a kind of roundabout method of solving this(there is probably a better way), so bear with me
use camera:ScreenPointToRay(x,y) or camera:viewportPointToRay(x,y) which will return a ray object, for the x and y arguments put them at wherever you want the npc to be on screen
you can get a point a set distance away from the camera by doin this

local ray = camera:ViewportPointToRay(500,500,1)--500,500 is placeholder
local _3dPos = Ray.Origin + Ray.Unit * 10--10 is distance from camera
local offset = camera.CFrame.Position - _3dPos
camera.CFrame = _3dPos+offset

sorry if i explained this bad
here’s my implementation(it works)
camerathing.rbxl (44.1 KB)

1 Like

After adjusting for my use case, this worked perfectly! Thanks so much!

1 Like

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