Search Unity

Question Bird flying backwards in Angry Birds Like Game Tutorial (Terrible Tweeters YouTube Tutorial)

Discussion in 'Scripting' started by RaeDickerson, Nov 25, 2021.

  1. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Happy Thanksgiving, all!

    I am following this tutorial on how to make a basic 2D game in Unity:



    Around 1:03:44, the instructor/YouTuber explains how cashing works etc. All he has us do is simply exchange a few objects in which we only reference them once. Before doing this, my little bird flew forward just fine and at the right speed...after exchanging the objects/variables* my bird now flies backwards and off the playing field. I haven't changed anything other than Cashing the RidigBody2d and SpriteRenderer. If I still had the code from before, I would gladly post it but all I have is my code from after I changed it. Could anyone explain to me how I broke this? I'm sure it's me who's done something wrong, though I've followed his tutorial verbatim.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Bird : MonoBehaviour
    7. {
    8.     Vector2 _startPosition;
    9.     Rigidbody2D _rigidbody2D;
    10.     SpriteRenderer _spriteRenderer;
    11.  
    12.     void Awake()
    13.     {
    14.         _rigidbody2D = GetComponent<Rigidbody2D>();
    15.         _spriteRenderer = GetComponent<SpriteRenderer>();
    16.     }
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         _startPosition = _rigidbody2D.position;
    22.         _rigidbody2D.isKinematic = true;
    23.     }
    24.  
    25.     void OnMouseDown()
    26.     {
    27.         _spriteRenderer.color = Color.red;  
    28.     }
    29.  
    30.     void OnMouseUp()
    31.     {
    32.         Vector2 currentPosition = _rigidbody2D.position;
    33.         Vector2 direction = _startPosition - currentPosition;
    34.         direction.Normalize();
    35.        
    36.         _rigidbody2D.isKinematic = false;
    37.         _rigidbody2D.AddForce(direction * 500);
    38.        
    39.         _spriteRenderer.color = Color.white;
    40.     }
    41.  
    42.     void OnMouseDrag()
    43.     {
    44.         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    45.         transform.position = new Vector3(mousePosition.x, mousePosition.y, transform.position.z);
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void Update()
    50.     {
    51.        
    52.     }
    53. }
    54.  
    Thank you in advance for any help.

    *I'm still learning C# terminology, my apologies for any confusion.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    There's many ways this could break... have you been working with backups? If so, revert to before you did whatever change that broke it. If not, you have to do these steps:

    1. figure out what actually causes the bird to fly (eg, trace the numeric data from finger-drag through to fling). This might involve adding pieces of code to print stuff out. See the very bottom of this post.

    2. reason about why it is wrong

    3. fix it

    Generally, this all falls under Step #2 of my "Two Steps To Do Tutorials Properly" guide. Let me offer you the guide here:

    -------------------------

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    -------------------------

    As far as working and keeping regular "step by step" backups, please consider using proper industrial-grade source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards

    -------------------------

    Whatever is happening, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Last edited: Nov 25, 2021
    RaeDickerson likes this.
  3. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8

    Thank you so much. I decided to delete everything and start over using your approach of making sure I can explain everything that's happening as it happens and not moving forward until I can. Thank you for all the in information on how to get Git to play right with Unity. I am attempting to set this up right now.
     
    Kurt-Dekker likes this.
  4. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Also, very cute doggo. I hope they get many pats and ear scratches.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You're welcome! As long as you have the right .gitignore, if you just periodically leave Unity and commit everything, even with a little note like "Got the targets spawning" or whatever, you will be leveraging a MASSIVE amount of git's awesomeness, and you can just build on it from there.
     
    RaeDickerson likes this.
  6. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Do you know if I can push using Rider?
     
  7. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Update: I deleted all the files, all the code, etc. Started the tutorial over verbatim, went slower this time. My bird is still flying backwards.

     
  8. Post your code. Previously you used the
    Vector2 direction = _startPosition - currentPosition;
    expression, which means your direction will point to the left (negative) if
    _startPosition
    is smaller than
    currentPosition
    (which is likely, since bird-flying games played left to right, usually) if I'm not mistaken.
    Have you tried to swap these two?
     
    RaeDickerson likes this.
  9. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    "New" code is below. No I haven't tried exchanging these because my plan tomorrow was to research everything in that line of code (since I'm new). However, I did change the direction multiplier to
    -500
    out of intuition : p. Your way seems more efficient.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Bird : MonoBehaviour
    7. {
    8.     Vector2 _startPosition;
    9.     Rigidbody2D _rigidbody2D;
    10.     SpriteRenderer _spriteRenderer;
    11.  
    12.     void Awake()
    13.     {
    14.         _rigidbody2D = GetComponent<Rigidbody2D>();
    15.         _spriteRenderer = GetComponent<SpriteRenderer>();
    16.     }
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         // saving starting position
    22.         _startPosition = _rigidbody2D.position;
    23.         // will not move due to physics in unity but bc of the code
    24.         _rigidbody2D.isKinematic = true;
    25.     }
    26.  
    27.     // color red when clicked
    28.     void OnMouseDown()
    29.     {
    30.         _spriteRenderer.color = Color.red;
    31.     }
    32.  
    33.     // change color to white when un-clicked
    34.     void OnMouseUp()
    35.     {
    36.         Vector2 currentPosition = _rigidbody2D.position;
    37.         Vector2 direction = _startPosition - currentPosition;
    38.         direction.Normalize();
    39.    
    40.         _rigidbody2D.isKinematic = false;
    41.         _rigidbody2D.AddForce(direction * -500);
    42.  
    43.         _spriteRenderer.color = Color.white;
    44.     }
    45.  
    46.     // Behavior for when we drag the birdie, will move bird to that position
    47.     void OnMouseDrag()
    48.     {
    49.         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    50.         transform.position = new Vector3(mousePosition.x, mousePosition.y, transform.position.z);
    51.     }
    52.  
    53.  
    54.     // Update is called once per frame
    55.     void Update()
    56.     {
    57.    
    58.     }
    59. }
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Yeah, I'm with lurk... it's possible that tutorial has an error for line 37 above.

    Whenever you have two quantities and you subtract them (such as these two vectors
    currentPosition
    and
    _startPosition
    ), to get the correct offset it is always:

    where you are going MINUS where you are

    That tells you "what do I have to add to me to get where I am going"

    That's why line 41 being -500 might be just to correct the flipped values on line 37... negating twice will leave you back to positive, but ideally you want to not negate at all. Every time you negate something that's an extra "wart" on your code that a) could be forgotten, or b) have other weird effects if these values are used for something else, or c) leave you puzzled later on if you only find one of the warts and not the other.
     
    RaeDickerson likes this.
  11. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Right, I was seriously using the -500 as like a temporary bandage until I could fix it. But are you saying I should exchange
    currentPosition
    and
    _startPosition
    ? Your analogy about subtracting "where I am from where I'm going" helped me to visualize this.
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I would do that, purely on the principle of:

    "if one wart only serves to undo another wart, why not remove both warts?"
     
    RaeDickerson likes this.
  13. RaeDickerson

    RaeDickerson

    Joined:
    Nov 23, 2021
    Posts:
    8
    Thank you so much! My lil bird is not only flying forward, but this tutorial forgot to mention adding a RigidBody2D to the crates & I ended up figuring that out on my own because of your tips hehe.

    I feel like a genius.


    Again, thank you SO much!!
     
    Kurt-Dekker likes this.
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    This is the kind of attitude that will do you so well in software engineering and pretty much any walk of life.

    Unity is a MASSIVE engine and architecture with decades of iterative design behind it, so nobody grasps it immediately, and unfortunately some find this off-putting.

    After a decade of using Unity, I still don't know it all and I never will! But... I know I can find it and I know how to keep looking, as well as use the best search keywords I can muster.

    With an attitude like yours and with sticking to it and asking lots of questions and looking at lots of how other people do it, you're gonna shine, as well as be a credit to the community.

    Welcome.
     
    RaeDickerson likes this.