Conteúdo do Curso
Unity for Beginners
Unity for Beginners
Move our Bird
This is the code that we have used to move our bird:
So let’s explain it in details: Our script is running on update function which is called every frame by Unity. It's where you can put code that needs to run continuously.
var oldVelocity = rb.velocity;
Declares a variable called "oldVelocity
" and assigns it the current velocity of the Rigidbody component attached to the Flappy Bird GameObject.
"rb
" is a reference to the Rigidbody component, which is responsible for simulating physics on the Flappy Bird.
if (Input.GetKeyDown(KeyCode.Space)
Check if the Space key is pressed down in this frame.
This condition ensures that the Flappy Bird jumps only once when the player presses the Space key.
oldVelocity .y = 6;
If the Space key is pressed down, sets the vertical (Y) component of the velocity to a certain value.
This makes the Flappy Bird jump upward when the Space key is pressed.
oldVelocity .x = 4;
Sets the horizontal(X) component of the velocity to a value.
This ensures that the horizontal movement of the Flappy Bird remains constant.
In Flappy Bird, the bird typically moves forward automatically, so this value represent its constant forward speed.
rb.velocity = oldVelocity ;
Assigns the modified velocity back to the Rigidbody component attached to the Flappy Bird GameObject.
This updates the velocity of the Flappy Bird, causing it to move according to the modified horizontal and vertical components.
The Flappy Bird's movement (jumping and forward motion) is controlled by adjusting its velocity.
What's Next
In the next few videos, we will delve into the topic of physics in game development, specifically focusing on Unity.
Obrigado pelo seu feedback!