Hello everyone, I’m trying to make a replay system that records and replays car movements in a row.
The game I am trying to clone is like the link below.
Sometimes, while the replay is taking place, there may be differences in the speeds of the repeating cars. It differs with the speed of the recorded car… I am suspicious of the update and the fixed update parts. how can i fix this?
Game:
https://www.youtube.com/watch?v=kLOUSD4Zkps*__
__//Movement script__*__
private void FixedUpdate()
{
StartRecording();
float horizontalMove = CrossPlatformInputManager.GetAxis("Horizontal");
transform.Rotate(new Vector3(0, 0, -horizontalMove * turningSpeed * Time.deltaTime));
rb.velocity = transform.up * forwardSpeed * Time.deltaTime;
}
//Record script
private void Update()
{
if (isRecording)
{
Record();
}
}
void Record()
{
AddFrameConstraint();
positionList.Add(transform.position);
rotationList.Add(transform.rotation);
velocityList.Add(carRigidbody.rb.velocity);
carTransformObject = new CarTransform(positionList, rotationList, velocityList);
}
private void AddFrameConstraint()
{
float frameConstraint = Mathf.Round(maxFrameSize / Time.deltaTime);
if (positionList.Count > frameConstraint)
{
positionList.RemoveAt(0);
}
if (rotationList.Count > frameConstraint)
{
rotationList.RemoveAt(0);
}
if (velocityList.Count > frameConstraint)
{
velocityList.RemoveAt(0);
}
}
//Replay script
private void Start()
{
CarTransform currentCarTransform = SpawnManager.replayCarToMovePoint[this];
positions = currentCarTransform.positions;
rotations = currentCarTransform.rotations;
velocity = currentCarTransform.velocity;
StartCoroutine(MoveRecordedPosition());
}
IEnumerator MoveRecordedPosition()
{
//initial pos and rot
transform.position = positions [0];
transform.rotation = rotations[0];
int targetPointIndex = 1;
Vector3 targetPosition = positions [targetPointIndex];
Quaternion targetRotation = rotations[targetPointIndex];
Vector3 rbVelocity = velocity[targetPointIndex];
while (true)
{
transform.position = targetPosition;
transform.rotation = targetRotation;
rb.velocity = rbVelocity;
targetPointIndex = (targetPointIndex + 1);
if (targetPointIndex == positions.Count - 1) { DestroyReplayCar(); }
targetPosition = positions [targetPointIndex];
targetRotation = rotations [targetPointIndex];
yield return null;
}
}