I have the following code to control a drone, I’m hoping to make this game into an obby but where you are a drone, similar to that one where you are a bird but you’re a drone insteasd.
Anyways the drone has unintended behavior when moving the mouse at the same time as holding any of the keyboard keys
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local dronesFolder = game.Workspace:WaitForChild('Drones')
local playerDrone = dronesFolder:WaitForChild(player.Name.." Drone")
local camera = workspace.CurrentCamera
camera.CameraSubject = playerDrone:WaitForChild("Handle")
local event = game.ReplicatedStorage:WaitForChild("DroneMovement")
local velocity = playerDrone.Handle.LinearVelocity
local mouse = player:GetMouse()
local XPower = 0
local YPower = 0
local ZPower = 0
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.W then
--event:FireServer("W", true)
ZPower = -20
end
if input.KeyCode == Enum.KeyCode.S then
-- event:FireServer("S", true)
ZPower = 20
end
if input.KeyCode == Enum.KeyCode.Q then
--event:FireServer("Q", true)
YPower = -20
end
if input.KeyCode == Enum.KeyCode.E then
--event:FireServer("E", true)
YPower = 20
end
if input.KeyCode == Enum.KeyCode.D then
--event:FireServer("D", true)
XPower = 20
end
if input.KeyCode == Enum.KeyCode.A then
--event:FireServer("A", true)
XPower = -20
end
velocity.VectorVelocity = velocity.Parent.CFrame:VectorToWorldSpace(Vector3.new(XPower, YPower, ZPower))
end)
mouse.Move:Connect(function()
local currentPos = velocity.Parent.Parent:GetPivot().Position
local targetPos = Vector3.new(mouse.Hit.X, currentPos.Y, mouse.Hit.Z)
local lookAtCFrame = CFrame.lookAt(currentPos, targetPos)
velocity.Parent.Parent:PivotTo(lookAtCFrame)
end)
UIS.InputEnded:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.W then
--event:FireServer("W", false)
ZPower = 0
end
if input.KeyCode == Enum.KeyCode.S then
--event:FireServer("S", false)
ZPower = 0
end
if input.KeyCode == Enum.KeyCode.Q then
--event:FireServer("Q", false)
YPower = 0
end
if input.KeyCode == Enum.KeyCode.E then
--event:FireServer("E", false)
YPower = 0
end
if input.KeyCode == Enum.KeyCode.D then
--event:FireServer("D", false)
XPower = 0
end
if input.KeyCode == Enum.KeyCode.A then
--event:FireServer("A", false)
XPower = 0
end
velocity.VectorVelocity = velocity.Parent.CFrame:VectorToWorldSpace(Vector3.new(XPower, YPower, ZPower))
end)
Just to make sure, you can only move in one direction at a time as the drone?
In terms of the mouse movement, it looks like every single time the player moves their mouse, you’re setting the drone’s orientation towards the physical spot the mouse moved to.
If you could clarify a bit on how you exactly want this to work, since I have never played the bird obby game you mentioned and I can’t currently check it, that would be great.
The drone is supposed to constantly rotate to face the mouse on the X and Z axis and the linear velocities should not cause anything to break, if the player rotates their mouse to the left when the player is holding W the drone should rotate to there and continue flying straight
Oh sorry I hadn’t seen that there was a video link. It looks like the center of mass of the drone is messed up. I had a similar error once with an animation I was working on. Are there any parts connected to the drone that are not physically attached to it? It looks like when the drone is rotating, it’s rotating around some pivot point that is not actually on the drone, but moved away from it.
I guess my first suggestion would just be to make sure no other parts are connected to the drone.
I’ve had similar issues with models imported from blender where the center of mass is completely messed up. I’d make sure the mesh itself is only on the drone. I’m not exactly sure what else could be causing this.
Ohhhhhhhhh I’m sorry I’m being kinda stupid right now. I think what’s going on is that the linear velocity isn’t adjusting quickly enough to the change in rotation, so it’s rotating and still sliding sideways and then kinda spazzing out due to that.
It looks like you’re changing the direction of the velocity only when an input changes. You should probably try and update it more frequently than that, so that rotations are taken account of as they happen.
Fixed, here is the functional code if anyone wants it:
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local dronesFolder = game.Workspace:WaitForChild('Drones')
local playerDrone = dronesFolder:WaitForChild(player.Name.." Drone")
local camera = workspace.CurrentCamera
camera.CameraSubject = playerDrone:WaitForChild("Handle")
local event = game.ReplicatedStorage:WaitForChild("DroneMovement")
local velocity = playerDrone.Handle.LinearVelocity
local mouse = player:GetMouse()
local XPower = 0
local YPower = 0
local ZPower = 0
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.W then
--event:FireServer("W", true)
ZPower = -20
end
if input.KeyCode == Enum.KeyCode.S then
-- event:FireServer("S", true)
ZPower = 20
end
if input.KeyCode == Enum.KeyCode.Q then
--event:FireServer("Q", true)
YPower = -20
end
if input.KeyCode == Enum.KeyCode.E then
--event:FireServer("E", true)
YPower = 20
end
if input.KeyCode == Enum.KeyCode.D then
--event:FireServer("D", true)
XPower = 20
end
if input.KeyCode == Enum.KeyCode.A then
--event:FireServer("A", true)
XPower = -20
end
end)
mouse.Move:Connect(function()
local direction = (mouse.Hit.Position - velocity.Parent.Position).unit
velocity.Parent.CFrame = CFrame.new(velocity.Parent.Position, velocity.Parent.Position + direction)
end)
UIS.InputEnded:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.W then
--event:FireServer("W", false)
ZPower = 0
end
if input.KeyCode == Enum.KeyCode.S then
--event:FireServer("S", false)
ZPower = 0
end
if input.KeyCode == Enum.KeyCode.Q then
--event:FireServer("Q", false)
YPower = 0
end
if input.KeyCode == Enum.KeyCode.E then
--event:FireServer("E", false)
YPower = 0
end
if input.KeyCode == Enum.KeyCode.D then
--event:FireServer("D", false)
XPower = 0
end
if input.KeyCode == Enum.KeyCode.A then
--event:FireServer("A", false)
XPower = 0
end
end)
game["Run Service"].Heartbeat:Connect(function()
velocity.VectorVelocity = velocity.Parent.CFrame:VectorToWorldSpace(Vector3.new(XPower, YPower, ZPower))
end)