Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make the Cframe of HumanoidRootPart go the opposite direction of the CurrentCamera CFrame?

Asked by 5 years ago
Edited 5 years ago

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?

1 answer

Log in to vote
1
Answered by 5 years ago

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.

0
Thank you, this helped a lot! Its facing the right way now, but now theres a new problem. The player doesn't face up or down anymore while flying like it used to, now its stuck facing forward/left/right. How do I make it so it faces all directions again? ShinyGriffin 129 — 5y
0
The BodyGyro's MaxTorque is infinite on all axis ShinyGriffin 129 — 5y
0
Are you certain you are putting the humanoid into the Flying state correctly? The character has its own gyro to keep it standing upright in the Running, Jumping, and other states where the character is expected to be vertical. You should use the Flying or Physics state when flying (which one depends on how you want collision to work while flying). EmilyBendsSpace 1025 — 5y
0
Thank you! I didn't know that the HumanoidStateType was counteracting the bodygryo. I changed the statetype to flying and now it works well. ShinyGriffin 129 — 5y
Ad

Answer this question