[Getting started with Unity] 9. Frame update

[Getting Started with Unity] Frame Update

    Hello everyone, I am Lampard~~

    Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks

(1) Obtain the attribute information of the mounted object

(1) Get name information

    First of all, in the C# code, this represents the script itself, and this.gameObject is the object that records the script mounted . Then to access the name property of the object, we can:

string name = this.gameObject.name;
Debug.Log("当前物体的名字是" + name);

    You can also directly omit the gameObject and use this.name to access it (a bit like cocos getChild)

string name = this.name;
Debug.Log("当前物体的名字是" + name);

    Look at the output effect after running, the current component is hung on the moon, so there is no problem with the output

(2) Global coordinates

    We can access the position information of the object through the transition component . The specific writing method is as follows:

Vector3 pos = this.transform.position;
Debug.Log("当前物体的位置坐标是" + pos);

    Take a look at the output:

    The access is accessed, but it seems to be different from the attribute value of the Moon object

     That's because the current moon is hung on the node of the Earth-Moon system. Let's look at the coordinate values ​​of the current "Earth-Moon system" node, and through conversion, we can get the result just printed. From this we also know that position is the global coordinate of the object to be taken rather than the local coordinate

(3) Local coordinates

    It is troublesome to verify that the coordinates are correct by converting them every time, so we can directly access the local coordinate information of the object through the localPosition attribute of transform

     Take a look at the results:

(4) Reset the coordinate position

    Now that the get has reached the coordinates, is it possible to set it in the next step?

     There is no doubt that it is possible. We can observe that the position is of the Vector3 type, so just replace it with a new one.

this.transform.localPosition = new Vector3(5f,5f,0);

(2) Frame update

(1) Use the update method

    Like cocos, unity also has an update method, which is executed every frame of the game. So how often is it executed? We can access it through the Time.deltaTime field

     Then execute and view the console box, and found that by default, it is about 0.005~0.01. That is, between 100 and 200 frames per second

    The frame rate is not a fixed value, it is related to the current state of the device, for example, when we play a game, when we encounter some gorgeous special effects, or when a text message comes in, the frame rate may drop. We can now set the frame rate of our demo through Application.targetFrameRate , and unity will try to execute according to the set frame rate

    For example, set Application.targetFrameRate = 60 now

    It can be seen that the execution time after setting is approximately maintained at 0.016, that is, 60 executions per second

(2) Set the value of the coordinates for each frame

    We can modify the coordinates of the object in the update method, so that the movement effect of the object can be realized. For example, this can make the x of the object move by 0.1 units every time the update is executed

    Note that the modified coordinates need to be copied to the localPosition attribute again, because Vector3 is a value type , when it is modified, a new object will be generated to record, and it cannot be directly modified on the original basis

    ok, the ball is moving now, but it needs to be taken into account, because the execution time of the update is not fixed, in order to achieve a uniform movement, we have to change the wording : for example, if we want to run 0.1 units per frame, then 60 frames per second means running 6 units per second

Well that's all for today, thanks for reading! ! !
Like and follow! ! !

Guess you like

Origin blog.csdn.net/cooclc/article/details/130047211