mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-25 05:20:17 +00:00
try to fix brakes ; refactor vehicle input
This commit is contained in:
parent
e988839038
commit
8318a67912
5 changed files with 95 additions and 54 deletions
|
@ -146,13 +146,13 @@ namespace SanAndreasUnity.Behaviours.Peds.States
|
||||||
|
|
||||||
m_model.VehicleParentOffset = Vector3.zero;
|
m_model.VehicleParentOffset = Vector3.zero;
|
||||||
|
|
||||||
var driveState = this.CurrentVehicle.Steering > 0 ? AnimIndex.DriveRight : AnimIndex.DriveLeft;
|
var driveState = this.CurrentVehicle.Input.steering > 0 ? AnimIndex.DriveRight : AnimIndex.DriveLeft;
|
||||||
|
|
||||||
var state = m_model.PlayAnim(AnimGroup.Car, driveState, PlayMode.StopAll);
|
var state = m_model.PlayAnim(AnimGroup.Car, driveState, PlayMode.StopAll);
|
||||||
|
|
||||||
state.speed = 0.0f;
|
state.speed = 0.0f;
|
||||||
state.wrapMode = WrapMode.ClampForever;
|
state.wrapMode = WrapMode.ClampForever;
|
||||||
state.time = Mathf.Lerp(0.0f, state.length, Mathf.Abs(this.CurrentVehicle.Steering));
|
state.time = Mathf.Lerp(0.0f, state.length, Mathf.Abs(this.CurrentVehicle.Input.steering));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
|
|
||||||
[SyncVar] float m_net_acceleration;
|
[SyncVar] float m_net_acceleration;
|
||||||
[SyncVar] float m_net_steering;
|
[SyncVar] float m_net_steering;
|
||||||
[SyncVar] float m_net_braking;
|
[SyncVar] bool m_net_isHandBrakeOn;
|
||||||
|
|
||||||
[SyncVar] float m_net_health;
|
[SyncVar] float m_net_health;
|
||||||
|
|
||||||
|
@ -184,9 +184,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
|
|
||||||
// local ped is occupying driver seat
|
// local ped is occupying driver seat
|
||||||
|
|
||||||
float oldAcc = m_vehicle.Accelerator;
|
var oldInput = m_vehicle.Input;
|
||||||
float oldBrake = m_vehicle.Braking;
|
|
||||||
float oldSteer = m_vehicle.Steering;
|
|
||||||
|
|
||||||
if (!GameManager.CanPlayerReadInput())
|
if (!GameManager.CanPlayerReadInput())
|
||||||
this.ResetInput();
|
this.ResetInput();
|
||||||
|
@ -196,16 +194,14 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
// why do we send input ?
|
// why do we send input ?
|
||||||
// - so that everyone knows if the gas/brake is pressed, and can simulate wheel effects
|
// - so that everyone knows if the gas/brake is pressed, and can simulate wheel effects
|
||||||
// - so that server can predict position and velocity of rigid body
|
// - so that server can predict position and velocity of rigid body
|
||||||
PedSync.Local.SendVehicleInput(m_vehicle.Accelerator, m_vehicle.Steering, m_vehicle.Braking);
|
PedSync.Local.SendVehicleInput(m_vehicle.Input);
|
||||||
|
|
||||||
// TODO: also send velocity of rigid body
|
// TODO: also send velocity of rigid body
|
||||||
|
|
||||||
if (!NetStatus.IsServer && !VehicleManager.Instance.controlInputOnLocalPlayer)
|
if (!NetStatus.IsServer && !VehicleManager.Instance.controlInputOnLocalPlayer)
|
||||||
{
|
{
|
||||||
// local player should not control input, so restore old input
|
// local player should not control input, so restore old input
|
||||||
m_vehicle.Accelerator = oldAcc;
|
m_vehicle.Input = oldInput;
|
||||||
m_vehicle.Braking = oldBrake;
|
|
||||||
m_vehicle.Steering = oldSteer;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,9 +209,11 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
{
|
{
|
||||||
if (NetStatus.IsServer)
|
if (NetStatus.IsServer)
|
||||||
{
|
{
|
||||||
m_net_acceleration = m_vehicle.Accelerator;
|
var vehicleInput = m_vehicle.Input;
|
||||||
m_net_steering = m_vehicle.Steering;
|
m_net_acceleration = vehicleInput.accelerator;
|
||||||
m_net_braking = m_vehicle.Braking;
|
m_net_steering = vehicleInput.steering;
|
||||||
|
m_net_isHandBrakeOn = vehicleInput.isHandBrakeOn;
|
||||||
|
|
||||||
m_net_health = m_vehicle.Health;
|
m_net_health = m_vehicle.Health;
|
||||||
|
|
||||||
// wheels
|
// wheels
|
||||||
|
@ -241,9 +239,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer &&
|
if (!this.IsControlledByLocalPlayer || (this.IsControlledByLocalPlayer &&
|
||||||
!VehicleManager.Instance.controlInputOnLocalPlayer))
|
!VehicleManager.Instance.controlInputOnLocalPlayer))
|
||||||
{
|
{
|
||||||
m_vehicle.Accelerator = m_net_acceleration;
|
m_vehicle.Input = new Vehicle.VehicleInput
|
||||||
m_vehicle.Steering = m_net_steering;
|
{
|
||||||
m_vehicle.Braking = m_net_braking;
|
accelerator = m_net_acceleration,
|
||||||
|
steering = m_net_steering,
|
||||||
|
isHandBrakeOn = m_net_isHandBrakeOn,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// update wheels
|
// update wheels
|
||||||
|
@ -274,28 +275,21 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
|
|
||||||
void ResetInput()
|
void ResetInput()
|
||||||
{
|
{
|
||||||
m_vehicle.Accelerator = 0;
|
var input = m_vehicle.Input;
|
||||||
m_vehicle.Steering = 0;
|
input.Reset();
|
||||||
m_vehicle.Braking = 0;
|
m_vehicle.Input = input;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadInput()
|
void ReadInput()
|
||||||
{
|
{
|
||||||
var customInput = CustomInput.Instance;
|
var customInput = CustomInput.Instance;
|
||||||
|
|
||||||
var accel = customInput.GetAxis("Vertical");
|
var vehicleInput = new Vehicle.VehicleInput();
|
||||||
var brake = customInput.GetButton("Brake") ? 1.0f : 0.0f;
|
vehicleInput.accelerator = customInput.GetAxis("Vertical");
|
||||||
var speed = Vector3.Dot(m_vehicle.Velocity, m_vehicle.transform.forward);
|
vehicleInput.isHandBrakeOn = customInput.GetButton("Brake");
|
||||||
|
vehicleInput.steering = customInput.GetAxis("Horizontal");
|
||||||
|
|
||||||
if (speed * accel < 0f)
|
m_vehicle.Input = vehicleInput;
|
||||||
{
|
|
||||||
brake = Mathf.Max(brake, 0.75f);
|
|
||||||
accel = 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_vehicle.Accelerator = accel;
|
|
||||||
m_vehicle.Steering = customInput.GetAxis("Horizontal");
|
|
||||||
m_vehicle.Braking = brake;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EnableOrDisableRigidBody()
|
public void EnableOrDisableRigidBody()
|
||||||
|
|
|
@ -11,14 +11,40 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
private Rigidbody _rigidBody;
|
private Rigidbody _rigidBody;
|
||||||
public Rigidbody RigidBody => _rigidBody;
|
public Rigidbody RigidBody => _rigidBody;
|
||||||
|
|
||||||
[Range(-1, 1)]
|
public struct VehicleInput
|
||||||
public float Accelerator;
|
{
|
||||||
|
public float accelerator;
|
||||||
|
public float steering;
|
||||||
|
public bool isHandBrakeOn;
|
||||||
|
|
||||||
[Range(-1, 1)]
|
public void Reset()
|
||||||
public float Steering;
|
{
|
||||||
|
this.accelerator = 0f;
|
||||||
|
this.steering = 0f;
|
||||||
|
this.isHandBrakeOn = false;
|
||||||
|
}
|
||||||
|
|
||||||
[Range(0, 1)]
|
public void Validate()
|
||||||
public float Braking = 1f;
|
{
|
||||||
|
if (float.IsNaN(this.accelerator))
|
||||||
|
this.accelerator = 0f;
|
||||||
|
if (float.IsNaN(this.steering))
|
||||||
|
this.steering = 0f;
|
||||||
|
this.accelerator = Mathf.Clamp(this.accelerator, -1f, 1f);
|
||||||
|
this.steering = Mathf.Clamp(this.steering, -1f, 1f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private VehicleInput m_input;
|
||||||
|
public VehicleInput Input
|
||||||
|
{
|
||||||
|
get => m_input;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
m_input = value;
|
||||||
|
m_input.Validate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Vector3 Velocity { get { return _rigidBody != null ? _rigidBody.velocity : Vector3.zero; } }
|
public Vector3 Velocity { get { return _rigidBody != null ? _rigidBody.velocity : Vector3.zero; } }
|
||||||
|
|
||||||
|
@ -156,22 +182,38 @@ namespace SanAndreasUnity.Behaviours.Vehicles
|
||||||
{
|
{
|
||||||
var vals = VConsts.Instance;
|
var vals = VConsts.Instance;
|
||||||
|
|
||||||
|
Vector3 velocity = this.Velocity;
|
||||||
|
|
||||||
|
float movementSign = 1f;
|
||||||
|
if (velocity == Vector3.zero)
|
||||||
|
movementSign = 0f;
|
||||||
|
else if (Vector3.Angle(velocity, this.transform.forward) > 90f) // going in reverse
|
||||||
|
movementSign = -1f;
|
||||||
|
|
||||||
|
float acc = m_input.accelerator;
|
||||||
|
float brake = m_input.isHandBrakeOn ? 1f : 0f;
|
||||||
|
if (acc != 0f && movementSign != 0f && Mathf.Sign(movementSign) != Mathf.Sign(acc))
|
||||||
|
{
|
||||||
|
acc = 0f;
|
||||||
|
brake = 1f;
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var wheel in _wheels)
|
foreach (var wheel in _wheels)
|
||||||
{
|
{
|
||||||
// apply steering
|
// apply steering
|
||||||
if (ShouldSteer(wheel))
|
if (ShouldSteer(wheel))
|
||||||
{
|
{
|
||||||
wheel.Collider.steerAngle = HandlingData.SteeringLock * Steering;
|
wheel.Collider.steerAngle = HandlingData.SteeringLock * m_input.steering;
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply motor torque
|
// apply motor torque
|
||||||
wheel.Collider.motorTorque =
|
wheel.Collider.motorTorque =
|
||||||
Accelerator * HandlingData.TransmissionEngineAccel
|
acc * HandlingData.TransmissionEngineAccel
|
||||||
* vals.AccelerationScale * DriveBias(wheel);
|
* vals.AccelerationScale * DriveBias(wheel);
|
||||||
|
|
||||||
// apply brake torque
|
// apply brake torque
|
||||||
wheel.Collider.brakeTorque =
|
wheel.Collider.brakeTorque =
|
||||||
Braking * HandlingData.BrakeDecel
|
brake * HandlingData.BrakeDecel
|
||||||
* vals.BreakingScale * BrakeBias(wheel);
|
* vals.BreakingScale * BrakeBias(wheel);
|
||||||
|
|
||||||
// update travel
|
// update travel
|
||||||
|
|
|
@ -68,19 +68,24 @@ namespace SanAndreasUnity.Net
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendVehicleInput(float acceleration, float steering, float braking) =>
|
public void SendVehicleInput(Vehicle.VehicleInput vehicleInput) =>
|
||||||
this.CmdSendingVehicleInput(acceleration, steering, braking);
|
this.CmdSendingVehicleInput(vehicleInput.accelerator, vehicleInput.steering, vehicleInput.isHandBrakeOn);
|
||||||
|
|
||||||
[Command]
|
[Command]
|
||||||
void CmdSendingVehicleInput(float acceleration, float steering, float braking)
|
void CmdSendingVehicleInput(float acceleration, float steering, bool isHandBrakeOn)
|
||||||
{
|
{
|
||||||
if (null == m_ped || null == m_ped.CurrentVehicle)
|
if (null == m_ped)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var v = m_ped.CurrentVehicle;
|
var vehicle = m_ped.CurrentVehicle;
|
||||||
v.Accelerator = acceleration;
|
if (null == vehicle)
|
||||||
v.Steering = steering;
|
return;
|
||||||
v.Braking = braking;
|
|
||||||
|
var input = new Vehicle.VehicleInput();
|
||||||
|
input.accelerator = acceleration;
|
||||||
|
input.steering = steering;
|
||||||
|
input.isHandBrakeOn = isHandBrakeOn;
|
||||||
|
vehicle.Input = input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -61,16 +61,16 @@ namespace SanAndreasUnity.Stats
|
||||||
{
|
{
|
||||||
List<System.Object> objects = new List<System.Object>(){
|
List<System.Object> objects = new List<System.Object>(){
|
||||||
vehicle.Velocity,
|
vehicle.Velocity,
|
||||||
vehicle.Accelerator,
|
vehicle.Input.accelerator,
|
||||||
vehicle.Braking,
|
vehicle.Input.isHandBrakeOn,
|
||||||
vehicle.Steering,
|
vehicle.Input.steering,
|
||||||
vehicle.AverageWheelHeight,
|
vehicle.AverageWheelHeight,
|
||||||
vehicle.NetTransform.netId,
|
vehicle.NetTransform.netId,
|
||||||
vehicle.NetTransform.syncInterval,
|
vehicle.NetTransform.syncInterval,
|
||||||
vehicle.NetTransform.ComponentIndex,
|
vehicle.NetTransform.ComponentIndex,
|
||||||
};
|
};
|
||||||
|
|
||||||
var texts = new List<string>() {"velocity", "accelerator", "braking", "steering", "average wheel height",
|
var texts = new List<string>() {"velocity", "accelerator", "is handbrake on", "steering angle", "average wheel height",
|
||||||
"net id", "sync interval", "component index"};
|
"net id", "sync interval", "component index"};
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,8 +81,8 @@ namespace SanAndreasUnity.Stats
|
||||||
texts.Add("\t" + w.Alignment);
|
texts.Add("\t" + w.Alignment);
|
||||||
objects.Add(
|
objects.Add(
|
||||||
w.Collider != null
|
w.Collider != null
|
||||||
? string.Format("travel {0} rpm {1} radius {2} motor torque {3} mass {4} is grounded {5}",
|
? string.Format("travel {0} rpm {1} radius {2} motor torque {3} brake torque {4} mass {5} is grounded {6}",
|
||||||
w.Travel, w.Collider.rpm, w.Collider.radius, w.Collider.motorTorque, w.Collider.mass, w.Collider.isGrounded)
|
w.Travel, w.Collider.rpm, w.Collider.radius, w.Collider.motorTorque, w.Collider.brakeTorque, w.Collider.mass, w.Collider.isGrounded)
|
||||||
: "");
|
: "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue