2025-01-29 22:55:46 +01:00

214 lines
6.6 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace Gley.UrbanSystem.Internal
{
/// <summary>
/// This class is for testing purpose only
/// It is the car controller provided by Unity:
/// https://docs.unity3d.com/Manual/WheelColliderTutorial.html
/// </summary>
[System.Serializable]
public class AxleInfo
{
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor;
public bool steering;
}
public class PlayerCar : MonoBehaviour
{
public InputActionReference vrJoystick;
public List<AxleInfo> axleInfos;
public Transform centerOfMass;
public float maxMotorTorque;
public float maxSteeringAngle;
IVehicleLightsComponent lightsComponent;
bool mainLights;
bool brake;
bool reverse;
bool blinkLeft;
bool blinkRifgt;
float realtimeSinceStartup;
Rigidbody rb;
public InputActionProperty joystick;
public InputActionAsset inputActions;
public GameObject steeringWheel;
private InputAction moveCar;
UIInput inputScript;
public Volume postProcessingVolume;
private Vignette vignette; // To hold the vignette effect
public float vignetteMin = 0.2f; // Minimum vignette intensity
public float vignetteMax = 0.6f; // Maximum vignette intensity
public float speedVignetteMultiplier = 0.01f; // How much speed affects vignette intensity
private void OnEnable()
{
joystick.action.Enable();
}
private void OnDisable()
{
joystick.action.Disable();
}
private void Start()
{
GetComponent<Rigidbody>().centerOfMass = centerOfMass.localPosition;
inputScript = gameObject.AddComponent<UIInput>().Initialize();
lightsComponent = gameObject.GetComponent<VehicleLightsComponent>();
lightsComponent.Initialize();
rb = GetComponent<Rigidbody>();
}
// finds the corresponding visual wheel
// correctly applies the transform
public void ApplyLocalPositionToVisuals(WheelCollider collider)
{
if (collider.transform.childCount == 0)
{
return;
}
Transform visualWheel = collider.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
collider.GetWorldPose(out position, out rotation);
visualWheel.transform.position = position;
visualWheel.transform.rotation = rotation;
}
public void UpdateSteeringWheel(float steering)
{
steeringWheel.transform.localEulerAngles = Vector3.forward * steering * 2 *-1;
}
public void FixedUpdate()
{
Vector2 joystickInput = vrJoystick.action.ReadValue<Vector2>();
float motor = maxMotorTorque * (inputScript.GetVerticalInput() + joystickInput.y);
float steering = maxSteeringAngle * (inputScript.GetHorizontalInput() + joystickInput.x);
UpdateSteeringWheel(steering);
#if UNITY_6000_0_OR_NEWER
var velocity = rb.linearVelocity;
#else
var velocity = rb.velocity;
#endif
float localVelocity = transform.InverseTransformDirection(velocity).z + 0.1f;
reverse = false;
brake = false;
if (localVelocity < 0)
{
reverse = true;
}
if (motor < 0)
{
if (localVelocity > 0)
{
brake = true;
}
}
else
{
if (motor > 0)
{
if (localVelocity < 0)
{
brake = true;
}
}
}
foreach (AxleInfo axleInfo in axleInfos)
{
if (axleInfo.steering)
{
axleInfo.leftWheel.steerAngle = steering;
axleInfo.rightWheel.steerAngle = steering;
}
if (axleInfo.motor)
{
axleInfo.leftWheel.motorTorque = motor;
axleInfo.rightWheel.motorTorque = motor;
}
ApplyLocalPositionToVisuals(axleInfo.leftWheel);
ApplyLocalPositionToVisuals(axleInfo.rightWheel);
}
UpdateVignetteEffect(localVelocity);
}
private void UpdateVignetteEffect(float localVelocity)
{
if (vignette != null)
{
// Calculate vignette intensity based on speed
float speed = Mathf.Abs(localVelocity);
float vignetteIntensity = Mathf.Clamp(vignetteMin + speed * speedVignetteMultiplier, vignetteMin, vignetteMax);
vignette.smoothness.Override(vignetteIntensity);
}
}
private void Update()
{
realtimeSinceStartup += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
mainLights = !mainLights;
lightsComponent.SetMainLights(mainLights);
}
if (Input.GetKeyDown(KeyCode.Q))
{
blinkLeft = !blinkLeft;
if (blinkLeft == true)
{
blinkRifgt = false;
lightsComponent.SetBlinker(BlinkType.BlinkLeft);
}
else
{
lightsComponent.SetBlinker(BlinkType.Stop);
}
}
if (Input.GetKeyDown(KeyCode.E))
{
blinkRifgt = !blinkRifgt;
if (blinkRifgt == true)
{
blinkLeft = false;
lightsComponent.SetBlinker(BlinkType.BlinkRight);
}
else
{
lightsComponent.SetBlinker(BlinkType.Stop);
}
}
lightsComponent.SetBrakeLights(brake);
lightsComponent.SetReverseLights(reverse);
lightsComponent.UpdateLights(realtimeSinceStartup);
}
}
}