Make Viewportframe Object Face Mouse on Screen

So today I will show you how to make viewportframe object face mouse on screen


Start off by inserting a part on workspace and set position to 0,0,0

Create another part, and make the front face face the part we created before.


Let’s call our part which will be put in the viewportframe Bose, and the white part Gerald
Gerald will be the part where the camera will share the cframe with
Clone the camera, and name it Newcam

Go to command bar, and type in workspace.Newcam.CFrame=workspace.Gerald.CFrame
image

Insert the newcam into the viewportframe, and bose into it as well
Set the viewportframe CurrentCamera to be newcam

You should now be able to see bose

Delete Gerald
And enter this script

while wait() do
    local v = script.Parent.ViewportFrame.Part
    local x,y,z = CFrame.new(Vector3.new(9,0,0),Vector3.new(game.Players.LocalPlayer:GetMouse().X,-game.Players.LocalPlayer:GetMouse().Y,1000)):ToOrientation()
    game:GetService("TweenService"):Create(v,TweenInfo.new(.4,Enum.EasingStyle.Quint),{Orientation = Vector3.new(x*25,-(100-((y*125)/5)),z)}):Play()
end

Let me break down the code for you.
So basically, the math is pretty simple
What it is doing is it’s taking the mouse screen pixel position, and dividing it by the screen pixel size, which allows this to work on all screen sizes.
It uses a very basic cframe method that lets a part face a position with two Vector3s.

A visualisation is here

The plane mouse is on is offset through a Z value, which can control how much bose is facing the camera
(Also I made a mistake in the visualisation, it’s bose not gerald)

The tween just basically makes it look better.


Here is the playtest

28 Likes

Wouldn’t it be better to run this just when your mouse moves with UserInputService.InputChanged instead of unnecessarily updating every ~1/30th of a second?

You would also be better off putting constants in variables so you don’t have a bunch of magic numbers. Helps with readability and configuration. They just feel so arbitrary.

1 Like

Thank you for your insight. As for your first query, I agree. It’s completely up to you if you want to use UserInputService.InputChanged.

For your second query, I’m not so sure magic numbers can arise from simply screen scaling, but I’m open to be proven wrong.

Sincerely, iSyriux

Ooooo I’m definitely going to use this for my game Thanks a ton!

1 Like

They are magic numbers, I have no idea why they are that amount or why it’s there.

Assign them to a variable doesn’t cost anything with the extra benefit of readability.

You should definitely consider cleaning up your code.

1 Like

Oh, you mean the multipliers? I discovered those were the best numbers to use while experimenting with rotation amplifiers. You can change them around if you so please to, but keep in mind, the rotation may look wonky.

Edit: To clarify, the ones multiplying the vector3 numbers are the magic numbers. Feel free to add a variable for them.

You saved my day with this one!