How can I rotate an Item in a Viewport using CFrame?

I want to make it so that the view on my object is rotated to the side and not the bottom pointing at the CFrame but the entire sword of it.

The Issue is that whenever I Uses CFrame.Angles() it either go left right, up down, and spin in a direction but it doesn’t rotate to the side

I have tried to make it CFrame.fromOreintation() and CFrame.FromAxis() but none of them work as intended.

Currently I have my code set to
cam.CFrame = Bb.CFrame * CFrame.new(0,Bb.Orientation, Bb.Orientation) * CFrame.Angles(0,math.rad(0),math.rad(0))

And the result is
Image
The Sword is
Image2

Instead of rotating the model, rotate the camera to face the sword. This is what the Developer hub viewport article recommends as it is more performant.

To make the camera face the sword you can use CFrame.lookat()
As I don’t know which face is the front of the sword I can’t give an exact solution but it would look something like this.

cam.CFrame = CFrame.lookat((sword.PrimaryPart.CFrame * CFrame.new(-4,0,0)).Position, sword.PrimaryPart.Position) 
-- use whatever positioning variable it is for the sword. I just assumed it was a model.

This will position the camera 4 studs away from the sword, facing the swords left side, assuming the side you showed in the screenshot is the left side.

if you wanted to angle the sword you could do this after positioning the camera.

cam.CFrame *= CFrame.angles(0,0,math.rad(15))

This Face Selector plugin will help you tell which face is the left side if needed.

I hope this helps!

Hey i think this could help you

btw if you wanna do it the normal way you can but if you think you want to make yourself easier you can use this module by EgoMoose

I looked into his code and did not quite understand what was going on

its basically that it calculates the distance that the camera need to go away from the model so that the model fits properly

if you think your current code works then that module isnt needed

I found out that it was the facing of the object and because the front was the bottom of the handle, it always faces the bottom of the sword. Is there a way to change the facing of the sword to a different side?

I did a bit of testing on a random object and it rotates as intended

you could probably mess around with the values of the CFrame to get the result you want

I finally got a result I wanted after messing with it a while

cam.CFrame = CFrame.lookAt((Bb.CFrame * CFrame.Angles(math.rad(90),math.rad(0),math.rad(0)) * CFrame.new(0,0,-4)).Position,Bb.Position) * CFrame.fromEulerAnglesXYZ(math.rad(0),math.rad(0),math.rad(45))

1 Like