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!
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.
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?
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?
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.
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.
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?