Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Move our Bird | Write your First Script
Unity for Beginners
course content

Course Content

Unity for Beginners

Unity for Beginners

1. Unity Introduction
2. Write your First Script
3. Unity Physics
4. Unity UI and Sounds
5. Polishing and Export your Game

book
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.

1. In the provided code, what does "rb.velocity" refer to?
2. What Unity class provides access to user input like keyboard presses?
3. What does "Input.GetKeyDown(KeyCode.Space)" do in the code?
In the provided code, what does "rb.velocity" refer to?

In the provided code, what does "rb.velocity" refer to?

Select the correct answer

What Unity class provides access to user input like keyboard presses?

What Unity class provides access to user input like keyboard presses?

Select the correct answer

What does "Input.GetKeyDown(KeyCode.Space)" do in the code?

What does "Input.GetKeyDown(KeyCode.Space)" do in the code?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt