SanAndreasUnity/Assets/Scripts/Behaviours/Vehicles/VehicleController.cs

60 lines
1.5 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using UnityEngine;
using SanAndreasUnity.Net;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.Behaviours.Vehicles
{
[RequireComponent(typeof(Vehicle))]
public class VehicleController : MonoBehaviour
{
2019-04-29 22:11:14 +00:00
private Vehicle m_vehicle;
2020-05-31 17:07:22 +00:00
private void Awake()
{
2019-04-29 22:11:14 +00:00
m_vehicle = GetComponent<Vehicle>();
2020-05-31 17:07:22 +00:00
}
private void Update()
{
2019-04-29 22:11:14 +00:00
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
this.ResetInput();
2020-05-31 17:07:22 +00:00
if (!GameManager.CanPlayerReadInput()) return;
var accel = Input.GetAxis("Vertical");
var brake = Input.GetButton("Brake") ? 1.0f : 0.0f;
2019-04-29 22:11:14 +00:00
var speed = Vector3.Dot(m_vehicle.Velocity, m_vehicle.transform.forward);
2020-05-31 17:07:22 +00:00
if (speed * accel < 0f)
{
brake = Mathf.Max(brake, 0.75f);
accel = 0f;
}
2019-04-29 22:11:14 +00:00
m_vehicle.Accelerator = accel;
m_vehicle.Steering = Input.GetAxis("Horizontal");
m_vehicle.Braking = brake;
2020-05-31 17:07:22 +00:00
}
void ResetInput()
{
2019-04-29 22:11:14 +00:00
m_vehicle.Accelerator = 0;
m_vehicle.Steering = 0;
m_vehicle.Braking = 0;
}
2020-05-31 17:07:22 +00:00
}
}