Change Fixed Timestep?
Hello everyone, i have been using unity for a few months (i apologize in advance for the inexperience) to create a driving simulator, so as to obtain information on the trajectory traveled, speed, acceleration etc.
In the script i made i used a FixedUpdate, and therefore the frequency is equal to 50 Hz (0.02).
I would like to change the frequency and i have tried both by changing the "Fixed Timestep" in the unity settings, and by introducing "Time.fixedDeltaTime" in the FixedUpdate. But in both cases there are repercussions on the physics of the vehicle, which i can no longer control.
Any suggestions on how to fix this?
Thanks in advance
using UnityEngine;
using System.IO;
public class Traiettoria : MonoBehaviour
{
public GameObject CAR;
public Rigidbody Car;
public float Posx;
public float Posy;
public float Posz;
public float TIME;
public float SPEED;
void Start()
{
Posx = CAR.transform.position.x-98;
Posy = CAR.transform.position.y;
Posz = CAR.transform.position.z;
TIME = Time.time;
SPEED = Car.velocity.magnitude;
Time.fixedDeltaTime = 0.5f;
}
void FixedUpdate()
{
Posx = CAR.transform.position.x-98;
Posy = CAR.transform.position.y;
Posz = CAR.transform.position.z;
TIME = Time.time;
SPEED = Car.velocity.magnitude;
Time.fixedDeltaTime = 0.5f;
WriteToFile(Posx, Posy, Posz, TIME, SPEED);
}
public static void WriteToFile(float Posx, float Posy, float Posz, float TIME, float SPEED)
{
string fileName = "_Traiettoria.txt";
string textToAdd = (Posx + " " + Posy + " " + Posz + " " + TIME + " " + SPEED + "\r");
using (StreamWriter writer = new StreamWriter(Application.streamingAssetsPath + fileName, true))
{
writer.Write(textToAdd);
writer.Close();
}
}
}
Answer by xxmariofer · Nov 17, 2020 at 01:28 PM
you are mixing concepts, simply leave the FixedUpdate for the physics, the fixedDeltaTime also controls how often physics get calculated, simply do something like this
void Start()
{
Posx = CAR.transform.position.x-98;
Posy = CAR.transform.position.y;
Posz = CAR.transform.position.z;
TIME = Time.time;
SPEED = Car.velocity.magnitude;
StartCoroutine(UpdateCarRoutine());
}
IEnumerator UpdateCarRoutine()
{
while(true)
{
Posx = CAR.transform.position.x-98;
Posy = CAR.transform.position.y;
Posz = CAR.transform.position.z;
TIME = Time.time;
SPEED = Car.velocity.magnitude;
WriteToFile(Posx, Posy, Posz, TIME, SPEED);
yield return new WaitForSeconds(0.5f);//SET HERE YOUR DESIRED FREQUENCY
}
}
public static void WriteToFile(float Posx, float Posy, float Posz, float TIME, float SPEED)
{
string fileName = "_Traiettoria.txt";
string textToAdd = (Posx + " " + Posy + " " + Posz + " " + TIME + " " + SPEED + "\r");
using (StreamWriter writer = new StreamWriter(Application.streamingAssetsPath + fileName, true))
{
writer.Write(textToAdd);
writer.Close();
}
}
Your answer
Follow this Question
Related Questions
What is the difference between Update() , LateUpdate() , FixedUpdate(), and when i should use them 3 Answers
How to do Inputs in Update and AddForce in FixedUpdate, infinite jumps ? 0 Answers
Why does FixedUpdate work when Update() doesn't? 2 Answers
Raycast2D in update? 1 Answer
Choppy camera, moving player in FixedUpdate and enemies through navmesh 0 Answers