Help newbie understand code

Hello everyone, I hope you all are having a great day. So i want to make a flappy bird game for my school project.
I am following the tutorial from Legacy Communities - IBM TechXchange Community .
And i am follow until the coding bird part. Nothing wrong with code, it’s work perfectly. But i don’t understand the whole code. Can anyone teach me what the function
of the code or give reference website so i can study it? thanks.

Here is thecode :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bird : MonoBehaviour {
public float jumpforce =4.1f;

Rigidbody2D mybody;
Animator myanim;

// Use this for initialization
void Start () {
myanim = GetComponent ();//làm rõ
mybody = GetComponent ();//làm rõ

}

// Update is called once per frame
void FixedUpdate () {
birdjump ();
}

void birdjump(){
if (Input.GetMouseButtonDown (0)) {
mybody.velocity = new Vector2 (mybody.velocity.x, jumpforce);
}
}
}

Pelase format your post to expect some help.
Code snippets goes into [code ] tags, use the ribbon on top of the posting editor here in the forum, you will find a code-tag symbol on the right side.

2 Likes

scripting API is good place to go, look up each keyword from the code,

and the regular documentation for components

Start(), FixedUpdate() are special unity methods that get executed

1 Like

I didn’t learned C#, also I don’t know how the things are exactly named :hushed:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bird : MonoBehaviour
{
    //variabe (this will moving speed up later)
    public float jumpforce = 4.1f;

    //the link (something like referenced variable) to the birds rigidbody will be stored here
    Rigidbody2D mybody;

    //the link to the birds Animator will be stored here
    Animator myanim;


    // Use this for initialization (is called once at start of the scene)
    void Start()
    {
        //here you will look at the gameobject bird (the script is added to it)
        //and search for the component Animator on it
        //and then make that myanim has link to the Animator on the gameobject (bird) stored
        //later, if you type "myanim." then the script knows, that you will work with the
        //Animator from this gameobject
        myanim = GetComponent<Animator>();//làm rõ

        //create link (reference) to the Rigidbody2D component
        mybody = GetComponent<Rigidbody2D>();//làm rõ

    }

    // FixedUpdate is called every 0.02 seconds
    void FixedUpdate()
    {
        //also every 0.02 seconds you will execute the birdjump function (method)
        birdjump();
    }

    void birdjump()
    {
        //now you are checking if the left mouse button was pressed
        if (Input.GetMouseButtonDown(0))
        {
            //if the left button was pressed

            //"mybody." you are saying: I will work with the Rigidbody2D
            //and you can change values from the rigidbody
            //also you can say that the moving speed should have an other direction (vector)

            //as example: mybody.velocity = new Vector2(0, 0); -> the object will have zero speed and just stays without to move
            //as example: mybody.velocity = new Vector2(0, 1); -> the object will move up (on y-axis) with speed  1
            //as example: mybody.velocity = new Vector2(-5, 5); ->
            //the object will move left (on x-axis) with speed 5 and will move up (on y-axis) with speed  5
            //also this will be diagonal moving left-up

            //here you are saying, that the bird shouldn't have other speed on x-axis (just allow that unity physics continue to control it)
            //and you are changing the moving speed on the y-axis. For the new speed value (on y-axis) you are taking
            //the value from the variable jumpforce
            //also the bird will go up
            mybody.velocity = new Vector2(mybody.velocity.x, jumpforce);
        }
    }
}
1 Like

Well first you have your name spaces that is imported into your script which each offers it’s own classes and constructors, built-in functionality.

// These are the NameSpaces you're importing into your project.

using System.Collections;  //  Deals with collections such as IEnumerators

using System.Collections.Generic; // Mostly used for List<>

using UnityEngine; // You'll use this 99% of the time

Next you have you Class and content. Everything you want to happen in your script occurs here.

// Whatever you name your script will be the Class name
// In your instance it's a poorly named "bird".
//May I encourage you to use Uppercase for classes? "Bird" instead
public class bird : MonoBehaviour {

// Here is the variables that are declared/initialized inside your script
public float jumpforce =4.1f;
// public = global and visible inside inspector
// float = a decimal based number value
// jumpForce is the name of your float variable.
// Since it's initialized upon declaration, this is known as "Hard coded"
// jumpForce = 4.1f;     4.1 is the decimal the "f" is for "float"

Rigidbody2D mybody;
// Rgidbody2D is a 2D built-in physics component.
// mybody (Would look better "myBody")
// mybody is the name of your RB variable
// Rigidbodies as most commonly used for movement

Animator myanim;
// Animator is a component attached to the GameObject
// This is like an Audio Player that stores the playlist (Basically)
// myanim (better if myAnim) is te variable name
// You will use the Animator to access animations/clips and play them

Now you’re entering a new phase zone, as I like to call it. This is after-loaded, after-starting up, now you are in the actual game and it is live and you’re playing now!

// Start is a Phase Unity goes through upon Startup
// Start phase happens after Awake()
// Unlike Awake(), the GameObjects should be loaded during this phase
void Start () {
myanim = GetComponent<Animator> ();//làm rõ
// You're initializing myanim by grabbing the component
// on the GameObject this script is attached to.

mybody = GetComponent<Rigidbody2D> ();//làm rõ
// Here you are initializing your Rigidbody2D variable
// by grabbing the Rigidbody2D component on the
// GameObject that this script is attached to

}

// [Update() is another Phase. This runs like 150 frames per second ? Maybe?]
// Update is called once per frame

// FixedUpdate() is great for calculations
// Similar to Update() only it runs 50 frames per second? Maybe? Eh, easy to look up. These are estimates lol
void FixedUpdate () {
birdjump ();
// Here you are calling a function named birdJump()
// This will run "50 frames per second" with my guestimation.
/ This will constantly be called throughout the game.
}

Finally, you’re getting past the phases of your program. Now you’re writing a mini sub-program to run at anytime you want. These are known as Methods and/or Functions. You don’t have to call these types every frame but you can, as demonstrated in your code. Which makes sense because your function is designed to listen for Left-Mouse click, which can also be translated as “Touching the screen” on a cell phone.

// This is a Function ( Also known as a Method )
// void is declaring that the Function will NOT be returning values
// Although not visible, this is a private void Function which means
// it is only accessible to this script. "private" is a protection level.
void birdjump(){

// IF statement is a Logical Evaluator. This is how you make a program think.
// Here you are "Listening" for a Keyboard INPUT from
// the mouse button.
// This particular call is for "Down" which is the moment the
//button is pressed down. This will fire once even if you hold the button down.
// GetButtonUp fires when button is released
// GetButton fires the entire time it is held down.
// This is looking for GetButtonDown(0)
// 0 is left click
// 1 is right click
// 2 is for mouse scroll-button.
if (Input.GetMouseButtonDown (0)) {

// Here you are adding a velocity physics to your Rigidbody2D component.
// You are setting it to a "new" position
// Vector2 = X-axis and Y-axis Vector3 = X,Y,Z
// This is reading your x-velocity and adding jumpforce the the Y axis.
// So looks like everytime you touch the phone screen
// your GameObject will move upwards whilst maintaining its
// X movement.
mybody.velocity = new Vector2 (mybody.velocity.x, jumpforce);
}
}
}

Hopefully you find this information useful :slight_smile:

4 Likes