Issues with changing BodyGyro properties

Hi, I’m trying to make a script ensuring that the flying bird’s face constantly faces the direction it is flying in, be it in the X, Y, or Z axis. Kind of like a shift lock.

I also tried this:



local cam = workspace.CurrentCamera
local Camlookvector = cam.CFrame

--This is how I went about it.

HumanoidRootPart.CameraGyro.CFrame = Camlookvector

--The problem is, the rotation does not go upwards and downwards, only left and right, and it's quite slow and inefficient.

--Any better ideas/help would be appreciated!

This is how it flies right now:
https://gyazo.com/5a8b6604ede1600b94810231dda6942a

Is this your actual code or just a sketch up? I don’t think cam.CFrame references the LookVector. Also, is CameraGyro a BodyGyro or AlignOrientation?

This is actual code. Also, it’s a BodyGyro. cam.CFrame seems to get something, it gets a bunch of different coordinates.

Turn up the P value in the BodyGyro then, it’s how responsiveness the BodyGyro acts.

1 Like

idk if this is actually the problem, but you set the Camlookvector as cam.CFrame when it should index the actual look vector, cam.CFrame.LookVector

1 Like

I discovered that if the BodyGyro has a position vector, it ignores it. It only tracks the orientation, so it doesn’t matter what the position is, which is why their current code works.

1 Like

I believe like @Katrist said. Going along with that, the BodyGyro seems to work similar to BodyPosition. The affected part will smoothly go into the destination position. Probably due to lack of power as set in your BodyGyro. You will need to add more power to it.

Also, isn’t the BodyGyro/BodyPosition an outdated thing? I think they have been changed to AlignOrientation/AlignPosition?

1 Like

You’re right, which is why I asked if they were using the alternative.

1 Like

I changed my method. Instead I’m using this, which gives much better results:

https://gyazo.com/92937e096df572ea7fe748225641c554

			X = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.X


			Y = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.Y


			Z = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.Z


			flightbodyPos.Velocity = Vector3.new(X,(Y - (offset/3)), Z)


			local pitch, yaw, roll = game.Workspace.CurrentCamera.CFrame:ToEulerAnglesYXZ()
			Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.CFrame.p) * CFrame.Angles(pitch, yaw, roll)

--flightbodyPos is a BodyPosition which is responsible for actually moving the flying bird.

However, as you can see by the GIF, it still gives very weird results. For example, if you look down, the bird sometimes tilts up. I think I need to recode the BodyPosition velocity, any ideas?

1 Like

Can you try to use the BodyGyro? The gyro is intended to control orientation.

			HumanoidRootPart.CameraGyro.CFrame = CFrame.new(Character.HumanoidRootPart.CFrame.p) * CFrame.Angles(pitch, yaw, roll)

I tried this, but the animal has such slow rotation and does not rotate upwards and downwards.

https://gyazo.com/d73f48d060e08f6dc994b899600a3718

Here’s a look at it.

Could you read my reply and test it out?

The P is already set to 3000000

2 Likes

Could you try using the LookVector and multiplying it?

Code:

flightbodyPos.Velocity = workspace.CurrentCamera.CFrame.LookVector * player.Character.Animal.Flight.Speed.Value

local pitch, yaw, roll = workspace.CurrentCamera.CFrame:ToEulerAnglesYXZ()
Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(pitch, yaw, roll)

--flightbodyPos is a BodyPosition which is responsible for actually moving the flying bird.
1 Like

Let me show you the entire code:

game:GetService("RunService").RenderStepped:Connect(function()
	if player.Character.Animal:FindFirstChild("Flight") then
		local flyanim = player.Character.Animal.Anims.Flight.Fly
		local flyAnimTrack = Humanoid:LoadAnimation(flyanim)	
		local flyanimplaying = false

		local diveAnim = player.Character.Animal.Anims.Flight.Dive
		local diveAnimTrack = Humanoid:LoadAnimation(flyanim)	
		local diveAnimplaying = false


		----


		local function takeOff()
			if IsRunning == true then
				--takeoffanim:Play()
				EventsFolder.FlightEvents.TakeOff:FireServer(true)
			end
		end

		local function stopFlying()
			EventsFolder.FlightEvents.TakeOff:FireServer(false)
			flyAnimTrack:Stop()
			TotalControl = true
			flyanimplaying = false
		end


		task.wait()
		if HumanoidRootPart:FindFirstChild("Flight") then
			local flightbodyPos = HumanoidRootPart:FindFirstChild("Flight")		

			TotalControl = false
			fallTime = 0
			fallTime2 = 0

			if flyanimplaying == false then
				flyAnimTrack:Play(0.5)
				flyanimplaying = true
			end


			local speed = player.Character.Animal.Flight.Speed.Value

			local X
			local Y
			local Z

			local cam = workspace.CurrentCamera
			local radX, radY, radZ = cam.CFrame:ToOrientation()

			local degX = (radX/math.pi) * 180
			local degY = (radY/math.pi) * 180
			local degZ = (radZ/math.pi) * 180

			local RunService = game:GetService("RunService")
			local Heartbeat = RunService.Heartbeat
			
			if degX < 0 then
				local GravityMultiplier = 2
				local PitchAmplitude = math.abs(degX/90) * 3

				if PitchAmplitude >= 1 then
					PitchAmplitude = math.abs(degX/90) * 3
					offset += GravityMultiplier * PitchAmplitude
				else
					offset = 0
				end
			end



			X = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.X


			Y = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.Y


			Z = player.Character.Animal.Flight.Speed.Value*workspace.CurrentCamera.CoordinateFrame.lookVector.Z


			flightbodyPos.Velocity = Vector3.new(X,(Y - (offset/3)), Z)


			HumanoidRootPart.CameraGyro.MaxTorque = Vector3.new(player.Character.Animal.Stats.WalkTurnSpeed.Value * 3000, player.Character.Animal.Stats.WalkTurnSpeed.Value * 3000 ,player.Character.Animal.Stats.WalkTurnSpeed.Value * 3000)
			HumanoidRootPart.CameraGyro.P = 3000000
			
			
			local pitch, yaw, roll = workspace.CurrentCamera.CFrame:ToEulerAnglesYXZ()
			Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position) * CFrame.Angles(pitch, yaw, roll)

			if Humanoid:GetState() == Enum.HumanoidStateType.Landed then
				stopFlying()
			end
		end

		uis.InputBegan:Connect(function(input)
			if (uis:GetFocusedTextBox()) then
				return; 
			end
			if input.KeyCode == Enum.KeyCode.Space then
				if player.Character.Animal:FindFirstChild("Flight") then
					takeOff()
				else
					stopFlying()
				end
			end
		end)

I coded the system so that the body position works with acceleration, hence why your code which sets the velocity to the look vector multiplies by the flight speed wouldn’t be ideal. It also makes the bird only fly one direction, because you didn’t call a Vector3, just a set variable.

1 Like

LookVector is a Vector3, I’m not sure what you’re talking about.

Oh sorry, I misread what you initially wrote. But yeah, I get this weird result nonetheless. (I used the code you sent)

https://gyazo.com/fe1ada8fead4d65c10a91049c766a180

It seems to drift in weird directions even though it should be going in the direction of the LookVector.

1 Like

Oh, I just found out why. I didn’t read your comment correctly and thought that the flightBodyPos was a BodyVelocity. Can you change it to a BodyVelocity?

Yeah I’ve since done this and it didn’t help.