mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-23 20:43:04 +00:00
85 lines
No EOL
2.2 KiB
C#
85 lines
No EOL
2.2 KiB
C#
using UnityEngine;
|
|
using SanAndreasUnity.Net;
|
|
using Mirror;
|
|
using SanAndreasUnity.Utilities;
|
|
|
|
namespace SanAndreasUnity.Behaviours.Vehicles
|
|
{
|
|
|
|
public class VehicleController : NetworkBehaviour
|
|
{
|
|
private Vehicle m_vehicle;
|
|
|
|
[SyncVar] int m_net_id = 0;
|
|
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
//m_vehicle = GetComponent<Vehicle>();
|
|
}
|
|
|
|
public override void OnStartClient()
|
|
{
|
|
base.OnStartClient();
|
|
|
|
if (!NetStatus.IsServer)
|
|
{
|
|
// load vehicle on clients
|
|
F.RunExceptionSafe( () => {
|
|
m_vehicle = Vehicle.Create(this.gameObject, m_net_id, null, this.transform.position, this.transform.rotation);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
|
|
var driverSeat = m_vehicle.DriverSeat;
|
|
|
|
if (null == driverSeat || null == driverSeat.OccupyingPed)
|
|
{
|
|
if (NetStatus.IsServer)
|
|
this.ResetInput();
|
|
return;
|
|
}
|
|
|
|
if (driverSeat.OccupyingPed != Ped.Instance)
|
|
return;
|
|
|
|
// local ped is occupying driver seat
|
|
|
|
if (!GameManager.CanPlayerReadInput())
|
|
this.ResetInput();
|
|
else
|
|
this.ReadInput();
|
|
|
|
PedSync.Local.SendVehicleInput(m_vehicle.Accelerator, m_vehicle.Steering, m_vehicle.Braking);
|
|
}
|
|
|
|
void ResetInput()
|
|
{
|
|
m_vehicle.Accelerator = 0;
|
|
m_vehicle.Steering = 0;
|
|
m_vehicle.Braking = 0;
|
|
}
|
|
|
|
void ReadInput()
|
|
{
|
|
var accel = Input.GetAxis("Vertical");
|
|
var brake = Input.GetButton("Brake") ? 1.0f : 0.0f;
|
|
var speed = Vector3.Dot(m_vehicle.Velocity, m_vehicle.transform.forward);
|
|
|
|
if (speed * accel < 0f)
|
|
{
|
|
brake = Mathf.Max(brake, 0.75f);
|
|
accel = 0f;
|
|
}
|
|
|
|
m_vehicle.Accelerator = accel;
|
|
m_vehicle.Steering = Input.GetAxis("Horizontal");
|
|
m_vehicle.Braking = brake;
|
|
}
|
|
|
|
}
|
|
} |