You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Driftology/Assets/Scripts/VehicleTracker.cs

318 lines
11 KiB
C#

using UnityEngine;
2 months ago
public class VehicleTracker : MonoBehaviour
2 months ago
{
[SerializeField] private float _leadOffset = 5f;
[SerializeField] private float _leadMultiplier = 0.1f;
[SerializeField] private float _sideOffset = 0f;
private bool _isOffTrack = false;
private float _offTrackTimer = 0f;
private float _trackerSpeed = 0f;
private float _distanceTraveled = 0f;
private float _speedLeadOffset = 50f;
private float _speedLeadMultiplier = 0.2f;
private float _offsetTimerRandom = 0f;
private RaycastHit _surfaceHit;
private Vector3 _deltaToWaypoint = Vector3.zero;
private Vector3 _previousPos = Vector3.zero;
private Vector3 _offsetToTarget = Vector3.zero;
private RoutePoint _currentWaypoint = new RoutePoint();
private Transform _targetTracker = null;
private VehicleController _linkedController = null;
public Vector3 TargetPos => _targetTracker != null ? _targetTracker.position : Vector3.zero;
private float VehicleSpeed => _linkedController != null ? _linkedController.LocalVelocity.z : 0f;
public WaypointCircuit waypointsCircuit;
public WaypointCircuit[] allwaypointCircuits;
2 months ago
private float _offsetSwitchTimer;
private void Start()
{
2 months ago
_linkedController = GetComponent<VehicleController>();
_targetTracker = new GameObject(name + " Tracker Target").transform;
_targetTracker.SetParent(transform);
_targetTracker.localPosition = new Vector3(0, -0.86f, 0);
2 months ago
_targetTracker.localRotation = Quaternion.identity;
_distanceTraveled = 0f;
_offsetTimerRandom = Random.Range(2.5f, 15f);
_offsetSwitchTimer = _offsetTimerRandom;
allwaypointCircuits = FindObjectsOfType<WaypointCircuit>();
for (int i = 0; i < allwaypointCircuits.Length; i++)
{
if (allwaypointCircuits[i].accepted == false)
{
allwaypointCircuits[i].accepted = true;
waypointsCircuit = allwaypointCircuits[i];
break;
}
}
2 months ago
}
private void FixedUpdate()
{
if (_linkedController == null || !_linkedController.Running)
{
_isOffTrack = false;
}
else
{
if (Physics.Raycast(transform.position - Vector3.down, -transform.up, out _surfaceHit, 10f))
_isOffTrack = _surfaceHit.collider.CompareTag("Ground");
else
_isOffTrack = false;
UpdateWaypointTracking();
}
2 months ago
if (_linkedController.Running && _linkedController.AIControlled)
{
_offsetSwitchTimer += Time.deltaTime;
2 months ago
if (_offsetSwitchTimer >= _offsetTimerRandom)
{
_offsetSwitchTimer = 0;
_sideOffset = 6f * Mathf.Sin(Time.time * _offsetTimerRandom);
2 months ago
}
}
if (_isOffTrack)
{
_offTrackTimer += Time.deltaTime;
2 months ago
if (_offTrackTimer > 3f)
{
_isOffTrack = false;
_offTrackTimer = 0f;
ResetToTrack();
}
}
else
{
_offTrackTimer = 0f;
}
}
private void UpdateWaypointTracking()
{
if (Time.deltaTime > 0f)
_trackerSpeed = VehicleSpeed;
_targetTracker.position = waypointsCircuit.GetRoutePoint(_distanceTraveled + _leadOffset + _leadMultiplier * _trackerSpeed).position + Vector3.right * _sideOffset;
_targetTracker.rotation = Quaternion.LookRotation(waypointsCircuit.GetRoutePoint(_distanceTraveled + _speedLeadOffset + _speedLeadMultiplier * _trackerSpeed).direction);
_currentWaypoint = waypointsCircuit.GetRoutePoint(_distanceTraveled);
_deltaToWaypoint = _currentWaypoint.position - transform.position;
if (Vector3.Dot(_deltaToWaypoint, _currentWaypoint.direction) < 0f)
_distanceTraveled += _deltaToWaypoint.magnitude * 0.5f;
_previousPos = transform.position;
}
private void ResetToTrack()
{
_linkedController.DisableVehicle();
transform.position = _targetTracker.position + new Vector3(0, 0.5f, 0);
transform.rotation = _targetTracker.rotation;
_linkedController.EnableVehicle();
}
}
//using Fusion;
//using UnityEngine;
//public class VehicleTracker : NetworkBehaviour
//{
// [SerializeField] private float _leadOffset = 5f;
// [SerializeField] private float _leadMultiplier = 0.1f;
// [SerializeField] private float _sideOffset = 0f;
// private bool _isOffTrack = false;
// private float _offTrackTimer = 0f;
// private float _trackerSpeed = 0f;
// private float _distanceTraveled = 0f;
// private float _speedLeadOffset = 50f;
// private float _speedLeadMultiplier = 0.2f;
// private float _offsetTimerRandom = 0f;
// private RaycastHit _surfaceHit;
// private Vector3 _deltaToWaypoint = Vector3.zero;
// private Vector3 _previousPos = Vector3.zero;
// private Vector3 _offsetToTarget = Vector3.zero;
// private RoutePoint _currentWaypoint = new RoutePoint();
// private Transform _targetTracker = null;
// private VehicleController _linkedController = null;
// public Vector3 TargetPos => _targetTracker != null ? _targetTracker.position : Vector3.zero;
// private float VehicleSpeed => _linkedController != null ? _linkedController.LocalVelocity.z : 0f;
// public WaypointCircuit waypointsCircuit;
// public WaypointCircuit[] allwaypointCircuits;
// private float _offsetSwitchTimer;
// //private void Start()
// //{
// // _linkedController = GetComponent<VehicleController>();
// // _targetTracker = new GameObject(name + " Tracker Target").transform;
// // _targetTracker.SetParent(transform);
// // _targetTracker.localPosition = Vector3.zero;
// // _targetTracker.localRotation = Quaternion.identity;
// // _distanceTraveled = 0f;
// // _offsetTimerRandom = Random.Range(2.5f, 15f);
// // _offsetSwitchTimer = _offsetTimerRandom;
// // waypointsCircuit=FindFirstObjectByType<WaypointCircuit>();
// //}
// public override void Spawned()
// {
// base.Spawned();
// // exactly your old Start() body:
// _linkedController = GetComponent<VehicleController>();
// _targetTracker = new GameObject(name + " Tracker Target").transform;
// _targetTracker.SetParent(transform);
// _targetTracker.localPosition = new Vector3(0,-0.86f,0);
// _targetTracker.localRotation = Quaternion.identity;
// _distanceTraveled = 0f;
// _offsetTimerRandom = Random.Range(2.5f, 15f);
// _offsetSwitchTimer = _offsetTimerRandom;
// allwaypointCircuits=FindObjectsOfType<WaypointCircuit>();
// for (int i = 0; i < allwaypointCircuits.Length; i++)
// {
// if (allwaypointCircuits[i].accepted==false)
// {
// allwaypointCircuits[i].accepted = true;
// waypointsCircuit = allwaypointCircuits[i];
// break;
// }
// }
// //waypointsCircuit = FindObjectOfType<WaypointCircuit>();
// }
// public override void FixedUpdateNetwork()
// {
// // only the host/stateauthority should drive the AI & resets
// if (!Object.HasStateAuthority)
// return;
// // copy your old FixedUpdate() ground check + waypoint update:
// if (_linkedController == null || !_linkedController.Running)
// {
// _isOffTrack = false;
// }
// else
// {
// // grounddetection
// if (Physics.Raycast(transform.position - Vector3.down, -transform.up, out _surfaceHit, 10f))
// _isOffTrack = _surfaceHit.collider.CompareTag("Ground");
// else
// _isOffTrack = false;
// UpdateWaypointTracking();
// }
// // copy your old Update() offtrack logic & sideoffset timer:
// if (_linkedController.Running && _linkedController.AIControlled)
// {
// _offsetSwitchTimer += Runner.DeltaTime; // use network delta
// if (_offsetSwitchTimer >= _offsetTimerRandom)
// {
// _offsetSwitchTimer = 0;
// _sideOffset = 6f * Mathf.Sin(Time.time * _offsetTimerRandom);
// }
// }
// if (_isOffTrack)
// {
// _offTrackTimer += Runner.DeltaTime;
// if (_offTrackTimer > 3f)
// {
// _isOffTrack = false;
// _offTrackTimer = 0f;
// ResetToTrack();
// }
// }
// else
// {
// _offTrackTimer = 0f;
// }
// }
// #region Commented
// //private void Update()
// //{
// // if (_linkedController.Running && _linkedController.AIControlled)
// // {
// // _offsetSwitchTimer += Time.deltaTime;
// // if (_offsetSwitchTimer >= _offsetTimerRandom)
// // {
// // _offsetSwitchTimer = 0;
// // _sideOffset = 6f * Mathf.Sin(Time.time * Time.deltaTime * _offsetTimerRandom);
// // }
// // }
// // if (_isOffTrack)
// // {
// // _offTrackTimer += Time.deltaTime;
// // if (_offTrackTimer > 3f)
// // {
// // _isOffTrack = false;
// // _offTrackTimer = 0f;
// // ResetToTrack();
// // }
// // }
// // else
// // {
// // _offTrackTimer = 0f;
// // }
// //}
// //private void FixedUpdate()
// //{
// // if (_linkedController == null || !_linkedController.Running)
// // {
// // _isOffTrack = false;
// // return;
// // }
// // // 👇 Restrict to local player or server
// // if (Physics.Raycast(transform.position - Vector3.down, -transform.up, out _surfaceHit, 10f))
// // {
// // _isOffTrack = _surfaceHit.collider.CompareTag("Ground");
// // }
// // else
// // {
// // _isOffTrack = false;
// // }
// // UpdateWaypointTracking();
// //}
// #endregion
// private void UpdateWaypointTracking()
// {
// if (Time.deltaTime > 0f)
// _trackerSpeed = VehicleSpeed;
// _targetTracker.position = waypointsCircuit.GetRoutePoint(_distanceTraveled + _leadOffset + _leadMultiplier * _trackerSpeed).position + Vector3.right * _sideOffset;
// _targetTracker.rotation = Quaternion.LookRotation(waypointsCircuit.GetRoutePoint(_distanceTraveled + _speedLeadOffset + _speedLeadMultiplier * _trackerSpeed).direction);
// _currentWaypoint = waypointsCircuit.GetRoutePoint(_distanceTraveled);
// _deltaToWaypoint = _currentWaypoint.position - transform.position;
// if (Vector3.Dot(_deltaToWaypoint, _currentWaypoint.direction) < 0f)
// _distanceTraveled += _deltaToWaypoint.magnitude * 0.5f;
// _previousPos = transform.position;
// }
// private void ResetToTrack()
// {
// _linkedController.DisableVehicle();
// transform.position = _targetTracker.position + new Vector3(0, 0.5f, 0);
// transform.rotation = _targetTracker.rotation;
// _linkedController.EnableVehicle();
// }
//}