Tilting your character while flying

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want my character to smoothly tilt in the direction they’re turning while flying. My flying system is based on pressing W and then moving your camera with your mouse to turn. This is what I’m trying to achieve:
    https://gyazo.com/8787cc8b5e3aa05bdd66f46a95df18ee

  2. What is the issue? My character is tilting but, when I stop flying, my character is standing in the air, even though the hip height has not been changed from the beginning to the end. I also get stuck in the orientation of the tilt if I stop flying while still tilting. My character also spasm out of control when I look directly up or down, which I think is the Body Gyro I have. This is what my problems looks like:
    https://gyazo.com/2c9cd59d0c189239e8dd10e404ebb173

  3. What solutions have you tried so far? I looked at other forums for how to tilt. I tried changing the C0 of the Motor6D in the LowerTorso, which works but it isn’t smooth. I tried doing something with Body Gyro but I just don’t have any math power for that. (I’m clueless on that part)

--[I only put in the necessary parts of my scripts here that's causing my problems]--
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local HRP = char.HumanoidRootPart

local bv = Instance.new('BodyVelocity')
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Name = 'FlightForce'
bv.Velocity = Vector3.new(0,0,0)
bv.Parent = HRP

local bg = Instance.new("BodyGyro")
bg.MaxTorque = Vector3.new(100000,100000,100000)
bg.Name = 'Flying Stabilizer'
bg.D = 7500
bg.P = 750000
bg.CFrame = HRP.CFrame --[Not really sure what to put here]
bg.Parent = HRP

---[The main movement stuff]---
game:GetService("RunService").RenderStepped:Connect(function()
     local CameraDirection = HRP.CFrame:ToObjectSpace(camera.CFrame).lookVector
     local characterPosition = HRP.CFrame.Position
     local charNextPosition = characterPosition + HRP.Velocity.Unit

     if wPressed then --[If player presses W, then wPressed = true and the player flies. Don't worry about this part]--
	     HRP:FindFirstChild('FlightForce').Velocity = camera.CFrame.LookVector * speed --[Flying based on Camera Direction]--

	     HRP.CFrame = CFrame.lookAt(characterPosition,charNextPosition)--[makes the character look based on the direction of the velocity. So if they are looking to the right, their character will turn to the right, but it won't TILT to the right]

	     char.LowerTorso.Root.C0 = CFrame.Angles(0,0,-math.asin(CameraDirection.X) * 2) --[The tilting part; This is the part that is making the player look like it's standing on air when not flying anymore. When I take it out, the standing on air doesn't happen]--

	     HRP:FindFirstChild('Flying Stabilizer').CFrame = HRP.CFrame --[not really sure what to put here]
     end	
end

I would prefer to do the titling by changing the CFrame of the Body Gyro but I don’t know how to do that, so I just manipulated the C0 of the LowerTorso’s Motor6D. And I’m not sure how I would fix that spasm of the character when looking straight up or down

6 Likes

You may want to consider using tween service to rotate your character. You simply have to input the rotated CFrame of the HumanoidRootPart of your character and play the Tween to give the tilting an animation.

Example:

local TweenService = game:GetService(“TweenService”)
local TweenInfo = TweenInfo.new()
local Tween = TweenService:Create(Character.HumanoidRootPart, TweenInfo, {Character.HumanoidRootPart.CFrame * CFrame.Angles(math.rad(80), 0, 0)})

— Code to detect when player turns
Tween:Play()
1 Like

to fix the player after they flight being tilted to a weird direction, save their original motor6d position then replace their motor6d c0 position like with the original one. idk how to do the part where you need it to smoothly tilt

1 Like

I would recommend setting Humanoid.PlatformStand to true so that the player doesn’t try to auto-correct it’s rotation. Then turn it back off when player lands

1 Like

How would I do that exactly? When you say “save” I dont really know what that means. But I basically created a variable at the top of the script outside the scope and then when they stop flying I just set the current C0 to the original one I created with the variable.

This is basically what I did. Idk if I’m doing it correctly

local originalC0 = char.LowerTorso.Root.C0

if isFlying == true then
   isFlying = false
   char.LowerTorso.Root.C0 = originalC0
end

game:GetService('RunService'):connect(function()
   if isFlying then
      --changing the C0
   end
end)

with your suggestion, I was able to make it tilt smoothly. For some reason I forgot about tween. I do have another problem. While the tilting is smooth, my character seems to be “micro shaking”, like phasing in and out. It happens only when I’m turning and it’ll happen even if I’m turning the slightest. When I’m just going straight it’s alright. You can see it here:
https://gyazo.com/75eb3389a65b0723a227b00d9e26a205
What’s causing that?

The micro shaking is very subtle so that may not be caused by the Tween. What I’m think is that you are using some sort of force or velocity as you play the Tween, possibly causing a micro shake. In that case, ensure that you engage only velocity and Tween one at a time.

Even so, you may still want to play around with the other arguments in TweenInfo just to make sure it’s not a Tween issue. What I may think your problem is, is that the easing style is not set to inout and the easing direction is not linear. Try:

local TweenInfo = TweenInfo.new(
    5 — Time for Tween to take place,
    Enum.EasingStyle.Linear,
    Enum.EasingDirecrion.InOut
)

From what I see, this detail is rather minute and you could carry on your project to complete it without fretting over it.

Yeah, It’s most likely not a Tweening issue as my TweenInfo is Linear and InOut.

I currently have this under RunService:

local TurningInfo = TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)
local TurningGoals = {C0 = CFrame.Angles(0,0,-math.asin(CameraDirection.X) * 4)} --[tilts the character when turning]

HRP.CFrame = CFrame.lookAt(characterPosition,charNextPosition)--[makes the character look based on the direction of the velocity]
			
HRP:FindFirstChild('FlightForce').Velocity = camera.CFrame.LookVector * speed
			
HRP:FindFirstChild('Flying Stabilizer').CFrame = HRP.CFrame
			
TS:Create(char.LowerTorso.Root, TurningInfo, TurningGoals):Play()

All my flight controlling stuff - moving, turning, looking up and down, tilting, playing the animations - is under RunService, so I’m not sure how I would make it so that I dont use a force as I play the Tween

Now that you say that the code you’ve sent is under runservice, I think it is a TweenInfo problem.

The issue is that you are constantly calling the Tween to play before the previous Tween can finish which causes this glitchy issue. What you should do is to put and if loop before the Tween:Play() to check if the Tween has completed before playing it again.

So, something like:

local isCompleted = true —Outside RunService loop
— Runservice event here
    if isCompleted == true then
        isCompleted = false
        Tween:Play()
    end 
    Tween.Completed:Connect(function()
        isCompleted = true
    end

Once again, if this doesn’t work, you may want to consider moving on, it’s too small of a detail to stop your project for.

I tried your solution, but I think it didn’t fix the shaking. It did however, change the smoothness of the tilting, making it less smooth and I had to change my numbers, like the time it takes to tween and the multiplier (the 4) so I could get the same feel. I’ll still search for solutions, and if I can’t I’ll just take your advice and move on. But I still think it has something to do with the Body Gyro

Do you also have any ideas on how I can fix the “standing on air and keeping orientation” bug? Whenever I stop flying, I land on the ground, but Im standing on air and if I stop flying while Im still tilting, it keeps that orientation and then I start walking like this:
https://gyazo.com/4ad061fa180cc66659e650d0f87652b0
I know it’s caused by the tilting code bc when I take it out, it doesn’t happen.

You should detect if the player has landed and use a tween to quickly reorientate the player straight up. You can do this by detecting if the player humanoid state has changed.

Example:

Humanoid.StateChanged:Connect(function(old, newState)
    if newState == Enum.HumanoidStateType.Landed then
        Tween = TweenService:Create(Character.HumanoidRootPart, TweenInfo, {Character.HumanoidRootPart.CFrame * CFrame.Angles(0,0,0)} -- I'm not exactly sure if 0,0,0 is vertical, you'll have to test
        Tween:Play()
    end
end)

I tested it and I found that if the player decided to come flying to the ground and hit it immediately before they stopped flying, they’ll still be on the air and keep their tilted orientation. This only works if there in the air, and not touching or smashing into objects

That would be because their state change was different. Print out the state change when the player does this action and include it as another if condition for newState.

There are countless states so it wouldn’t be surprising if another state was changed instead of Landed.

I’m not sure if what I did is good for performance or whatever, but under the RunService, I made it so that if isFlying == false then change the current C0 to the original one. It does what I want by immediately changing the C0 when they stop flying, even if they’re flying in the ground

game:GetService('RunService'):connect(function()
    if isFlying == false then
        if char.LowerTorso.Root.C0 ~= originalC0 then
		   char.LowerTorso.Root.C0 = originalC0
	    end
    end
end)

But maybe that could all be prevented if I don’t change the C0 at all and use Body Gyro instead, so that this doesn’t happen
https://gyazo.com/377686edaf215c42d7757a519a098438
But idk

You should definitely restrict your movement options to the minimum possible. That means aim to use just one movement option, i.e. choose between CFrame and Body gyro. Don’t use both, it’ll just be more work for you.

I planned to use Body Gyro to do all the tilting, but I didn’t understand how. But now that Body Gyro is already in my script, if I take it out, then the character will start to spasm like before whenever it hits something or looks straight up and down. Right now, the CFrame of the Body Gyro is set to the HRP and that’s all I’ve done with it.

I’m a little confused. Are you still encountering any problems with this set-up?

Yes I am. Since I changed the C0 of the LowerTorso to do the tilting, whenever the player stops moving (while still flying), they keep their orientation as well. This was part of the problem of why they stood in the air and had a weird rotation when not flying. It’s in the link I posted in my reply to myself.