Currently, I’ve been messing with cars working on PGS Physics, and I used some baseline code I found on the DevHub. One of the main complaints I have with the method I’m currently working with, is a small bit of input lag, to the point where everyone I’ve talked to who tests my cars have said it’s to the point of being noticeable.
My current code is as such:
local car = seat.Parent
local leftDrive = car.RearAxle.LeftDrive
local rightDrive = car.RearAxle.RightDrive
local steerHingeR = car.AxleR.SteerHingeR
local steerHingeL = car.AxleL.SteerHingeL
local function onChanged (property)
if property == "Steer" then
local steeringAngle = seat.Configuration.SteerAngle.Value
local turnSpeed = seat.Configuration.TurnSpeed.Value
steerHingeR.TargetAngle = steeringAngle * seat.Steer
steerHingeL.TargetAngle = steeringAngle * seat.Steer
steerHingeR.AngularSpeed = turnSpeed
steerHingeL.AngularSpeed = turnSpeed
end
if property == "Throttle" then
local maxSpeed = seat.Configuration.MaxSpeed.Value
local torque = seat.Configuration.Torque.Value * 1000
leftDrive.AngularVelocity = maxSpeed * seat.Throttle
rightDrive.AngularVelocity = -maxSpeed * seat.Throttle
rightDrive.MotorMaxTorque = torque
leftDrive.MotorMaxTorque = torque
end
end
seat.Changed:connect(onChanged)
Any help to reduce some of this latency would be greatly appreciated. Have considered that a while loop would have less latency, because of it’s constant running, right? My concern would be a limit on how many cars I could have running realistically before it destroys the server.
If this is a Server script then that may be the cause of input lag.
Try setting network ownership to the driver then handling user input from a local script.
That will also give you the flexibility of making custom control schemes.
In practice here, I’m confused how I use this though tbh.
Also, it seems like Network Ownership is auto-set on vehicles. The only case this is tricky is when a passenger messes things up, but I have no passenger seats in my vehicles, so that’s not a problem.
That’s interesting, I didn’t know network ownership was automatically set. But that kind of makes sense anyway.
In my case when a user requests a car - I spawn the car by the user, set network ownership to that user car:SetNetworkOwnership(player)
Then, I place the Local Driver script I have made (that handles controls and movement of the vehicle) into the player and enable it.
Since LocalScripts run the user’s machine there should be no delay in (e.g.) requesting to steer left and performing that action directly on the vehicle.
Whereas with a Server Script - the script has to listen to the VehicleSeat (which takes user input), perform the action, then replicate that action to all the users.
My latency isn’t from the physics of the vehicle I believe. I think it’s more just the waiting to get input from the vehicle seat, which is got from the player. So it’s just, latency of having the vehicleseat as a middle man.
I actually had the exact same problem with a car system of my own. When driving, you have NetworkOwnership over the vehicle, therefore you can use a local script for turning, I personally did this based on MoveVectors so I can have controller and mobile support.
You can still use a vehicle seat, just do all turning locally, act as if FE doesn’t exist.
Put the local script in the player backpack when they get in the car and remove it when they get out of it. Dying will automatically remove the script.