Search Unity

My snakes are flying

Discussion in 'Scripting' started by Marscaleb, Sep 2, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    So, it's been a long time coming, but I finally decided to update my project from Unity 4.7 or something to 5.1.

    I expected there to be problems with things that didn't make it through the transitions properly, and the first thing I have found is that I have enemies that are just... flying. Or perhaps, they are slowly falling up. But yeah, I have these snake enemies and they are just slowly floating up and away like they swallowed some upsidasium. Normally they just slither back and forth and fall if they walk off an edge like a koopa troopa.
    This is a 2D game by the way.

    I've got a lot of code to check through, but I figure there is a good chance someone has seen something similar to this (other than Samuel L Jackson of course) so I thought I would ask to see if someone has some clues to help me jump straight to the problem area.

    Other information:
    -My scripts open with a check to see if they are on screen, and basically all of their behavior is never executed if they are not on-screen. But I have discovered that even when they are off screen they still continue to float upwards.
    -My player character is unaffected by this floating phenomenon. I use a lot of special movement code for him though, and he bypasses most of the normal physics.
    -I have some frog enemies that hop around, and they just get stuck in the air. They don't fall nor do they float.
     
  2. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Okay, I've been going through my code, and I can't find anything that sounds like it would trigger this.

    There is nothing in my code that is adding any extra velocity. In fact, when they are not on screen the velocity of their rigidbody is set to zero.

    Something is causing the rigidbody2D to ignore and even reverse gravity.
     
  3. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    I don't have the answer, but Unity has a built-in gravity setting iirc, perhaps you can tweak that as a workaround, if you can't find a code issue.

    At least if you have a bug, it's an awesome one with flying snakes ;)
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    This may not work for you but I had issues with stuff I did in an older version of unity when I tried to continue work in the latest version. The scripts looked fine but if I added a debug line then saved & went back into unity the script updated & all these other changes occurred even though it didn't when I first tried to run the game. Maybe if you try the same thing it might force the updating?
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Have you checked if all scripts have updated properly to the new version? There are some potentially breaking changes that could cause your issues. (for example any .rigidbody calls should be changed to .GetComponent<RigidBody>() )
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    @tedthebug I just tried that, it made no change. Thanks anyway.

    @Timelog Yes, Unity automatically updated those scripts when I changed to the new version. Everything looks fine, particularly the code relevant to rigidbodies.

    Code (CSharp):
    1. void Update () {
    2.  
    3.         ClippingCheck();
    4.         CheckIfActive();
    5.  
    6.         if (!bActive) {
    7.             GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
    8.             return;
    9.         }
    10.  
    11.         ...
    12.    
    13.     }
    14.  
    15. void FixedUpdate ()
    16.     {
    17.  
    18.         if (health <= 0)
    19.             return;
    20.  
    21.         int move = 0;
    22.  
    23.         CheckIfActive();
    24.  
    25.         if ( bActive && !bInKnockBack) {
    26.             if (facingRight)
    27.                 move = 1;
    28.             else
    29.                 move = -1;
    30.         }
    31.  
    32.         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
    33.     }
    It looks fine to me.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
  8. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I appreciate the article. I did enjoy reading it, but he's not having the same problems as me, and what he did to solve his biggest problem is how I already work.
     
  9. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    Okay, I've been trying some stuff today, and now I have a problem with my snakes falling. :p

    The first thing I tried was changing my code so that the commands to add velocity occur in Update instead of FixedUpdate. (And yes, I did multiply the speed by time.deltatime.) This produced a result where they no longer moved forward, at all. Even if I multiplied their speed by 300, they didn't move. Except to slowly float up.
    Huh.

    Next I noticed that I didn't have problems with some objects floating, so I ran some tests. I had on older version of a simple enemy that uses the same script as the snakes, but it wasn't having this problem. I took one of the snakes, copied all the stats and attributes from the simple enemy, but it didn't make a difference.

    So next I tried rebuilding the snake. I built a snake from scratch, and set all of its attributes EXACTLY the same as the snakes I had already made. I created an exact duplicate from scratch; the only thing that was different was that it had a different name and was not associated as a prefab. When I tested it, I found it had no problems with the physics; it behaved exactly as it should.

    EXCEPT!

    This new snake keeps walking off ledges.

    I have a variable in my basic enemy AI script (called bRedShell) that dictates if they will turn around when they hit an edge or if they will keep walking. To determine if they are about to walk off an edge, I have a second groundCheck in the front of the enemies, and if it does not detect ground then the actor is not front-grounded. And so, if (bRedShell && !bFrontGrounded && bGrounded){ Flip(); }.

    But yet my new snake isn't doing that. The Flip function works because it still flips if it hits a wall. The ground check works because the animation properly changes when the snake is on ground or not. And the bRedShell boolean is set properly. But yet the snake just walks right off of the edges anyway.

    So my old snakes are flying, and my new snake is falling.

    EDIT:
    Wait, it turns out that my new snake is actually NOT animating proper to show him being on ground or not. I had to slow down his gravity to see it, but as he falls he still uses the "grounded" animation. (Meanwhile the old snakes still use the "in air" animation as they continuously float.)

    The code for checking the ground is as follows:

    Code (CSharp):
    1. protected void ClippingCheck (){
    2.         bCenterGrounded = Physics2D.OverlapCircle(centerGroundCheck.position, clipCheckRadius, whatIsGround);
    3.         bRearGrounded = Physics2D.OverlapCircle(rearGroundCheck.position, clipCheckRadius, whatIsGround);
    4.         bFrontGrounded = Physics2D.OverlapCircle(frontGroundCheck.position, clipCheckRadius, whatIsGround);
    5.  
    6.         bGrounded = (bCenterGrounded || bRearGrounded || bFrontGrounded);
    7.         bFullyGrounded = (bFrontGrounded && bRearGrounded && bCenterGrounded);
    8.      
    9.         bHitWall = Physics2D.OverlapCircle(wallCheck.position, clipCheckRadius, whatIsGround);
    10.  
    11.         anim.SetBool ("Ground", bGrounded);
    12.     }
    But this is the same code that they player uses; the function is in a base class that both the player and the enemies use, and they player has no problems with detecting the ground. But now the enemies are.
     
    Last edited: Sep 5, 2015
  10. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    *Sigh* I have no clue why this simply ground check isn't working.
    I guess I'll just have to revert to Unity 4.