LookVector To CFrame.fromOrientation - Gun Sway

I’m trying to create gun sway and I just need to find out how to convert a look vector

tried looking it up but I can’t find anything out

You just need to Convert a LookVector (SWAY_SPRING.Position) to an x, y, z orientation

I’m not exactly sure why you are doing this but there are probably more efficient ways to use the look vector. Either way lets get into it. By x y z orientation I assume you mean euler angles. It’s impossible to get all of the euler angles with simply a look vector because a look vector is a direction, which are described by the x and y parts of a euler angle. However the z describes the roll of a rotation. There is no way to derive the roll from a look vector. So assuming the roll should just be 0, you could derive the other components of the euler angles using trignometry like so:

local lookVector -- however you are getting the lookvector
local zAngle = 0
local yAngle = math.atan2(-lookVector.X,-lookVector.Z)
local xAngle = math.asin(lookVector.Y)

If you are just trying to derive a cframe from a look vector, don’t use this method. Instead construct it by getting the right and up vectors and then use CFrame:FromMatrix()

5 Likes

Ok sorry, this is kinda turning into an X/Y problem.
I’m making gun sway and I set the target of the spring to the camera look vector every render step. I’m trying to convert that look vector back into CFrame.
Sorry for the inconvenience, but I’m fairly sure that if @EgoMoose saw this he could help me.

Well there’s a few ways to go about it. If you check my FPS tutorial at the end i posted a place that has sway. Iirc i didn’t use look vectors and instead used mouse delta on the screen.

The problem that you’re facing is that you’re trying to use a spring to represent a rotation. By nature springs reach their target by traveling straight to it. That doesn’t work well with rotation unfortunately so you either need to do some complex math or take a different approach.

3 Likes

Ok i will look into it later.
BTW thanks for the quick reply

1 Like

Oops sorry kinda forgot about this. It worked amazingly :smiley: I should have reviewed over your place file sooner. My fault but thanks for the help.

1 Like