- Home /
Beginners question about transform.Translate and float values
Hi there,
I have a game object in my scene that I want to travel to the right using the transform.Translate function.
My understanding is that you put float values into the x, y and z parameters of the function in order to make a game object travel a certain distance on the x, y and z axis every frame?
So if I wrote the following in the Update function of a script that is attached to the game object:
void Update(){
transform.Translate(1f,0,0);
}
Does that mean the game object will travel a distance of 1 float per frame on the X-axis? I believe Unity projects run at a 60fps frame rate so that would mean that the GO would travel 60 floats to the right per second? But what does travelling 1 float actually mean? Does each square that can be seen in the grid in the scene view represent a float value?
Any help would be appreciated.
Answer by xxmariofer · Apr 21, 2021 at 08:27 AM
Hello,
forget about the "1 float", float is a type, a type use for numbers. data types are not that simple, and you should take a look into c# basics, but think of floats as numbers with decimals.
Second, in unity games dont run at 60fps, they could be running at a higher frame rate, but it is common to cap the fps to 60 (not allways and not necesary)
Third, unity doesnt use "meters" they use "units", if you use
transform.Translate(1,0,0);
you move in the x axis 1 unit.
I understand datatypes to an extent. I understand that the int datatype is used for integer values and that the float datatype is used for decimal values.
So when using the transform.Translate function I can make a game object travel a distance measured in units? Am I right in saying that these units are represented by the small squares that are shown in the scene view in the editor as you can see in this screenshot?: https://ibb.co/5LdmRFM
Also is there a way in which I can find out how many realtime fps my project is running during runtime?
Yes, the default grid every square is a unit.
You can find the time between frames with
Time.deltaTime
this returns the value from the last frame to current frame, so if you want the movement to be frameindependant you could do
transform.Translate(1 * Time.deltaTime,0,0);
this will move the robot 1 unit per second, instead of 1 unit per frame.
Your answer
