Started learning to work in Unity3D on a training video and there was a code in it (which did not work):

if (Input.GetKeyDown(KeyCode.W)) rigidbody.velocity.y = 8; 

Climbing on the Internet and trying several options it all worked for me, only when I replaced the code with this:

 Rigidbody rb = GetComponent<Rigidbody>(); if (Input.GetKeyDown(KeyCode.W)) rb.velocity = new Vector3(0, 8, 0); 

Why instead, the first option does not work, or option:

 Rigidbody rb = GetComponent<Rigidbody>(); if (Input.GetKeyDown(KeyCode.W)) rb.velocity.y = 8; 

    1 answer 1

    Because time is running, everything changes. And if earlier in the old versions of the software product it was possible to write some constructions, then in later versions something changes to others.

    So, for example, a rigidbody.velocity (and also audio, renderer, collider, and more) could be written in Unity version 4. With version 5, everything happens by calling the necessary components via GetComponent<SOME_COMPONENT>(); in C # and GetComponent(SOME_COMPONENT); in js

    Similar to velocity.y . Perhaps it was possible to write like this before, but now the assignment of a direction or speed or everything connected with Vector3 (and not only a vector) is happening wholly, i.e. through a call to new Vector3(0, 8, 0)

    • one
      Is there any good literature on new Unity standards or programming on the engine as a whole? - Enwinkel
    • one
      @Enwinkel Well, in general, everything that is new in the product is usually written directly in the issue at the office. site ...... here for example unity3d.com/ru/unity/whats-new/unity-5.0 ... there you can find the way Removed quick property accessors... way Removed quick property accessors... just about access via GetComponent ... so KMK is the only literature on new chips ............ and the literature in general so far I can only show it here. stackoverflow.com/a/609901/191482 ...... well, and this list to freak out: gcup.ru/forum/8-46855-1 . - Alexey Shimansky
    • @Enwinkel the funny thing is that I literally took out the screw for diagnostics an hour ago (and I don’t know now when I plug it), in which the list of books is drawn for reading: D xs, the truth is their adequacy. need to learn - Alexey Shimansky
    • yes order, I think the list on the stackOverflow that you threw off is enough =) - Enwinkel
    • @Enwinkel added another list of 20 pieces to the section on the book ...... ru.stackoverflow.com/a/609901/191482 ..... and a description of the list. I think it will be useful in the future - Alexey Shimansky