// Update is called once per frame void Update() { // Time since the ghost started float time = Time.realtimeSinceStartup - _startTime; // Index of the data according to time int index = Mathf.Clamp((int)(time * freq), 0, _data != null ? _data.Count - 2 : 0); // Read and apply data if (run && ((time < duration && index < _data.Count - 1) || _startTime == 0) && _data != null) { if (_startTime == 0) { _startTime = Time.realtimeSinceStartup; } GhostData d = _data[index]; GhostData df = _data[index + 1]; Vector3 pos = new Vector3(d.position[0], d.position[1], d.position[2]); Vector3 posf = new Vector3(df.position[0], df.position[1], df.position[2]); transform.position = Vector3.Lerp(pos, posf, (time * freq) - index); Quaternion rot = new Quaternion(d.rotation[0], d.rotation[1], d.rotation[2], d.rotation[3]); Quaternion rotf = new Quaternion(df.rotation[0], df.rotation[1], df.rotation[2], df.rotation[3]); transform.rotation = Quaternion.Lerp(rot, rotf, (time * freq) - index); if (_vehicle != null) { _vehicle.Throttle = d.throttle; _vehicle.Steering = d.steering; _vehicle.boosting = d.boost; _vehicle.Drift = d.drift; } if (_rb != null) { Vector3 speed = new Vector3(d.speed[0], d.speed[1], d.speed[2]); Vector3 speedf = new Vector3(df.speed[0], df.speed[1], df.speed[2]); _rb.velocity = Vector3.Lerp(speed, speedf, (time * freq) - index); } } }
public IEnumerator RecordCoroutine(bool allowOverTime = false) { WaitForSeconds wait = new WaitForSeconds(1.0f / freq); Debug.Log("Recording ghost for " + _vehicle.name); while (!requestStop && _data.Count <= _data.Capacity) { GhostData d = new GhostData(); d.position[0] = _vehicleT.position[0]; d.position[1] = _vehicleT.position[1]; d.position[2] = _vehicleT.position[2]; d.rotation[0] = _vehicleT.rotation[0]; d.rotation[1] = _vehicleT.rotation[1]; d.rotation[2] = _vehicleT.rotation[2]; d.rotation[3] = _vehicleT.rotation[3]; d.speed[0] = _vehicleR.velocity[0]; d.speed[1] = _vehicleR.velocity[1]; d.speed[2] = _vehicleR.velocity[2]; d.throttle = _vehicle.Throttle; d.steering = _vehicle.Steering; d.boost = _vehicle.boosting; d.drift = _vehicle.Drift; _data.Add(d); yield return(wait); if (_data.Count == _data.Capacity && allowOverTime) { _data.Capacity += freq * 10; } } Debug.Log("Finished recording ghost for " + _vehicle.name); }