I made a flying script and I'm trying to figure out how to make the direction of the character go in the direction the player is looking.
local RP = player.Character.HumanoidRootPart while Flying do rs.RenderStepped:wait() RP.CFrame = CFrame.new(RP.Position,workspace.CurrentCamera.CFrame.p) RP.BodyVelocity.Velocity = HumanoidRootPart.CFrame.lookVector*(flyspeed) RP.BodyGyro.CFrame = HumanoidRootPart.CFrame end
Everything else about the script works fine, but it makes the player's character fly towards the screen. I'm not too familiar with doing math with Vector3's and CFrames. How would I make the character fly in the direction the camera is facing and not towards the camera?
This line:
CFrame.new(RP.Position,workspace.CurrentCamera.CFrame.p)
Is constructing a CFrame that points from the character back to the camera. If you swapped these two arguments, it will point the direction you want, from camera to character:
CFrame.new(workspace.CurrentCamera.CFrame.p,RP.Position)
BUT... you don't need this CFrame at all. You're not using the BodyMovers correctly, and once you are (explained below), you'll only need the camera's CFrame
When using a BodyGyro + BodyVelocity on a character, you should not be setting CFrame of the HumanoidRootPart directly. When you do, neither the BodyGyro nor the BodyVelocity can do anything, because you're overriding them every frame by explicitly setting the position and orientation of the part they are meant to be controlling! This means your body movers are doing nothing right now, and clients cannot smooth out character movements using interpolation. If you currently see other players flying in a jerky way, much less smoothly than your own character, this is why.
So, remove the call that sets RP.CFrame. If your BodyGyro and BodyVelocity are set up correctly, everything should get a whole lot smoother because you'll now have client-side interpolation that the setting of the CFrame was preventing.
You don't need to even construct that extra CFrame from RP.Position and camera position. You can just set the BodyGyro CFrame to the CFrame of the camera if what you want is the character facing away from the camera:
BodyGyro.CFrame = game.Workspace.CurrentCamera.CFrame
BodyGyro ignores the translation (position) components of the CFrame, so you don't even need to subtract them away.
Depending upon how the character is oriented in your flying animation, you may want to tilt the character away from the camera's orientation, like this:
BodyGyro.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.fromOrientation(-math.rad(30),0,0)
You can fiddle with that -30 degrees to give any other amount of tilt you need, this choice was arbitrary on my part.