Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How Do I Fix This Boolean Glitch?

Discussion in '2D' started by Pixelkitty, Jan 12, 2015.

  1. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Here's a doozy. Can't figure it out for the life of me. Once I die, grounded is permanently stuck on false. If I set it true after I die using the Inspector, crouching will set it to false as well as dying. I can't jump at all!

    PlayerController:

    Code (CSharp):
    1. void Update ()
    2. {
    3.     if (grounded)
    4.     {
    5.         if (Input.GetButton ("Down"))
    6.         anim.SetBool ("Crouch", true);
    7.  
    8.         else
    9.         anim.SetBool ("Crouch", false);
    10.     }
    11.    
    12.     else
    13.     {
    14.         if (Input.GetButton ("Down"))
    15.         this.rigidbody2D.gravityScale = fastfall;
    16.  
    17.         else
    18.         this.rigidbody2D.gravityScale = normalfall;
    19.     }
    20. }
    21.  
    22.     void FixedUpdate ()
    23.     {
    24.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    25.  
    26.         if (Input.GetButton ("Jump"))
    27.         {
    28.             grounded = false;
    29.             anim.SetBool ("grounded", false);
    30.         }
    31.        
    32.         float move = Input.GetAxis ("Horizontal");
    33.        
    34.         anim.SetFloat ("Speed", Mathf.Abs (move));
    35.        
    36.         if (move > 0 && !facingRight)
    37.             Flip ();
    38.         else if (move < 0 && facingRight)
    39.             Flip ();
    40.  
    41.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    42.  
    43.         if (platform.GetComponent<PlatformTop>().pressure1)
    44.         {
    45.             grounded = true;
    46.             anim.SetBool ("grounded", true);
    47.            
    48.             if (Input.GetButtonDown ("Jump"))
    49.             {
    50.                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
    51.             }
    52.         }
    53.     }
    PlatformTop:

    Code (CSharp):
    1.  
    2. public class PlatformTop : MonoBehaviour
    3. {
    4.     public GameObject player;
    5.     public bool pressure1;
    6.        
    7.     void OnTriggerEnter2D (Collider2D col)
    8.     {
    9.         if (col.tag == "Player1")
    10.         {
    11.             pressure1 = true;
    12.         }
    13.     }
    14.  
    15.     void OnTriggerExit2D (Collider2D col)
    16.     {
    17.         pressure1 = false;
    18.     }
    19. }
    RespawnNew:

    Code (CSharp):
    1. public class RespawnNew : MonoBehaviour
    2. {
    3.  
    4.     public GameObject player1;
    5.     public Transform respawn;
    6.    
    7.     void OnTriggerEnter2D (Collider2D col)
    8.     {
    9.         if (col.tag == "Player1")
    10.         {
    11.             player1.GetComponent<PlayerController>().dead = true;
    12.             StartCoroutine (Die());
    13.         }
    14.     }
    15.  
    16.     IEnumerator Die ()
    17.     {
    18.         yield return new WaitForSeconds(2);
    19.         player1.transform.position = respawn.transform.position;
    20.         player1.GetComponent<PlayerController>().dead = false;
    21.     }
    22. }
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Do not put code for your character in your platforms you may have hundreds of these and its code that isn't needed, it should be controlled through your character and will lead to bugs.

    You seem to be having issues getting your character controller working so I suggest going through the tutorial Unity has on 2D Character Controllers. I followed this tutorial when I started using Unity and it helped a lot.
     
  3. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Okay, I'll check it out. Thanks.

    EDIT: Okay, new problem. I can't jump at all after removing PlatformTop, and grounded is constantly set to false. There is NOTHING I can see wrong with my script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.    
    9.     Animator anim;
    10.  
    11.     public float jumpForce = 50f;
    12.     public float normalfall = 2f;
    13.     public float fastfall = 5f;
    14.     public bool grounded;
    15.     public bool dead;
    16.     public GameObject platform;
    17.  
    18.     void Start ()
    19.     {
    20.         anim = GetComponent<Animator>();
    21.        
    22.         this.rigidbody2D.gravityScale = normalfall;
    23.     }
    24.  
    25.     void Update ()
    26. {
    27.     if (grounded)
    28.     {
    29.         if (Input.GetButton ("Down"))
    30.         anim.SetBool ("Crouch", true);
    31.  
    32.         else
    33.         anim.SetBool ("Crouch", false);
    34.     }
    35.    
    36.     else
    37.     {
    38.         if (Input.GetButton ("Down"))
    39.         this.rigidbody2D.gravityScale = fastfall;
    40.  
    41.         else
    42.         this.rigidbody2D.gravityScale = normalfall;
    43.     }
    44. }
    45.  
    46.     void FixedUpdate ()
    47.     {
    48.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    49.        
    50.         float move = Input.GetAxis ("Horizontal");
    51.        
    52.         anim.SetFloat ("Speed", Mathf.Abs (move));
    53.        
    54.         if (move > 0 && !facingRight)
    55.             Flip ();
    56.         else if (move < 0 && facingRight)
    57.             Flip ();
    58.  
    59.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    60.     }
    61.  
    62.     void OnCollisionStay ()
    63.     {
    64.             grounded = true;
    65.             anim.SetBool ("grounded", true);
    66.            
    67.             if (Input.GetButtonDown ("Jump"))
    68.             {
    69.                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
    70.                 grounded = false;
    71.                 anim.SetBool ("grounded", false);
    72.             }
    73.     }
    74.  
    75.     void Flip()
    76.     {
    77.         facingRight = !facingRight;
    78.         Vector3 theScale = transform.localScale;
    79.         theScale.x *= -1;
    80.         transform.localScale = theScale;
    81.     }
    82. }
     
    Last edited: Jan 13, 2015
  4. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    I promise that I'll try more things on my own after this, I just want to get this crap-fest out of the way first.
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    OnCollisionStay() is the 3D function and will be fired as long as player is touching anything that is a 3D collider not set to be a trigger.
    Use OnCollisionEnter2D(Collider2D other) and check if the collider is ground and if it is set grounded to true. This will only trigger once.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collider2D other)
    2. {
    3.     if (other.CompareTag("PutTagNameHere"))
    4.     {
    5.         grounded = true;
    6.         anim.SetBool("grounded", true);
    7.     }
    8. }
    9.  
    10. void OnCollisionExit2D(Collider2D other)
    11. {
    12.     if (other.CompareTag("PutTagNameHere"))
    13.     {
    14.         grounded = false;
    15.         anim.SetBool("grounded", false);
    16.     }
    17. }
    18.  
    Make move a class variable, and add another boolean called jumping.
    then update your Update() and FixedUpdate()

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (grounded)
    4.     {
    5.        //put all code that you can do on the ground here.
    6.        if (Input.GetButton("Down"))
    7.            anim.SetBool("Crouch", true);
    8.        else
    9.            anim.SetBool("Crouch", false);
    10.        
    11.        if (Input.GetButton("Jump"))      
    12.            jumping = true;
    13.     }
    14.     else
    15.     {
    16.         //put anything that you can do while in the air here
    17.         if (Input.GetButton("Down"))
    18.             this.rigidbody2D.gravityScale = fastfall
    19.         else
    20.             this.rigidbody2D.gravityScale = normalfall;
    21.     }
    22.  
    23.     move = Input.GetAxis("Horizontal");
    24.     anim.SetFloat("Speed", Mathf.Abs(move));
    25.  
    26. }
    27.  
    28. void FixedUpdate()
    29. {
    30.     rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    31.     anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    32.  
    33.     if (move > 0 && !facingRight)
    34.         Flip();
    35.     else if (move < 0 && facingRight)
    36.         Flip();
    37.  
    38.     if (jumping)
    39.     {
    40.         rigidbody2D.AddForce(new Vector2(0, jumpForce));
    41.         jumping = false;
    42.     }
    43. }
    44.  
    I am using the CompareTag function because using other.tag == "ground" will generate garbage, more specifically other.tag will return a copy of the string and not a reference(strings are hell on the garbage collector if you don't know what to do with them.) CompareTag does the == internally so it uses the actual string to do the compare.

    This is only one way to do what you are trying to accomplish. Follow the tutorial it will show you how to make a character controller that is more robust.
     
  6. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    I forgot about 2D. Crap. Alright, I'll leave you be 'cause I got this from here on out.
     
  7. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Actually, I'm going to have to do this from scratch now. Whatever you told me to do caused about 10 errors. Maybe I did it wrong?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public class move : MonoBehaviour
    7. {
    8.     public float maxSpeed = 10f;
    9.     bool facingRight = true;
    10.  
    11.     Animator anim;
    12.  
    13.     public float jumpForce = 50f;
    14.     public float normalfall = 2f;
    15.     public float fastfall = 5f;
    16.     public bool grounded;
    17.     public bool dead;
    18.     public GameObject platform;
    19.     public bool jumping;
    20.  
    21.     void Start ()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.        
    25.         this.rigidbody2D.gravityScale = normalfall;
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (grounded)
    31.         {
    32.             //put all code that you can do on the ground here.
    33.             if (Input.GetButton("Down"))
    34.                 anim.SetBool("Crouch", true);
    35.             else
    36.                 anim.SetBool("Crouch", false);
    37.            
    38.             if (Input.GetButton("Jump"))    
    39.                 jumping = true;
    40.         }
    41.         else
    42.         {
    43.             //put anything that you can do while in the air here
    44.             if (Input.GetButton("Down"))
    45.             this.rigidbody2D.gravityScale = fastfall;
    46.             else
    47.             this.rigidbody2D.gravityScale = normalfall;
    48.         }
    49.        
    50.         move = Input.GetAxis("Horizontal");
    51.         anim.SetFloat("Speed", Mathf.Abs(move));
    52.        
    53.     }
    54.    
    55.     void FixedUpdate()
    56.     {
    57.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    58.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    59.        
    60.         if (move > 0 && !facingRight)
    61.             Flip();
    62.         else if (move < 0 && facingRight)
    63.             Flip();
    64.        
    65.         if (jumping)
    66.         {
    67.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    68.             jumping = false;
    69.         }
    70.     }
    71.  
    72.     void OnCollisionEnter2D(Collider2D other)
    73.     {
    74.         if (other.CompareTag("Player1"))
    75.         {
    76.             grounded = true;
    77.             anim.SetBool("grounded", true);
    78.         }
    79.     }
    80.    
    81.     void OnCollisionExit2D(Collider2D other)
    82.     {
    83.         if (other.CompareTag("Player1"))
    84.         {
    85.             grounded = false;
    86.             anim.SetBool("grounded", false);
    87.         }
    88.     }
    89.  
    90.     void Flip ()
    91.     {
    92.         facingRight = !facingRight;
    93.         Vector3 theScale = transform.localScale;
    94.         theScale.x *= -1;
    95.         transform.localScale = theScale;
    96.     }
    97. }
    98. }
     
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    move is a class in your code. Here is a fixed version.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float move;
    8.     public float maxSpeed = 10f;
    9.     bool facingRight = true;
    10.  
    11.     Animator anim;
    12.  
    13.     public float jumpForce = 50f;
    14.     public float normalfall = 2f;
    15.     public float fastfall = 5f;
    16.     public bool grounded;
    17.     public bool dead;
    18.     public GameObject platform;
    19.     public bool jumping;
    20.  
    21.     void Start ()
    22.     {
    23.         anim = GetComponent<Animator>();
    24.  
    25.         this.rigidbody2D.gravityScale = normalfall;
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (grounded)
    31.         {
    32.             //put all code that you can do on the ground here.
    33.             if (Input.GetButton("Down"))
    34.                 anim.SetBool("Crouch", true);
    35.            else
    36.                 anim.SetBool("Crouch", false);
    37.  
    38.            if (Input.GetButton("Jump"))  
    39.                jumping = true;
    40.         }
    41.         else
    42.         {
    43.             //put anything that you can do while in the air here
    44.             if (Input.GetButton("Down"))
    45.                 this.rigidbody2D.gravityScale = fastfall;
    46.             else
    47.                 this.rigidbody2D.gravityScale = normalfall;
    48.         }
    49.  
    50.         move = Input.GetAxis("Horizontal");
    51.         anim.SetFloat("Speed", Mathf.Abs(move));
    52.  
    53.     }
    54.  
    55.     void FixedUpdate()
    56.     {
    57.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    58.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    59.  
    60.         if (move > 0 && !facingRight)
    61.             Flip();
    62.         else if (move < 0 && facingRight)
    63.             Flip();
    64.  
    65.         if (jumping)
    66.         {
    67.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    68.             jumping = false;
    69.         }
    70.     }
    71.  
    72.     void OnCollisionEnter2D(Collider2D other)
    73.     {
    74.         if (other.CompareTag("Player1"))
    75.         {
    76.             grounded = true;
    77.            anim.SetBool("grounded", true);
    78.         }
    79.     }
    80.  
    81.     void OnCollisionExit2D(Collider2D other)
    82.     {
    83.         if (other.CompareTag("Player1"))
    84.         {
    85.             grounded = false;
    86.             anim.SetBool("grounded", false);
    87.         }
    88.     }
    89.  
    90.     void Flip ()
    91.     {
    92.         facingRight = !facingRight;
    93.         Vector3 theScale = transform.localScale;
    94.         theScale.x *= -1;
    95.         transform.localScale = theScale;
    96.     }
    97. }
    98.  
     
  9. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    "Make move a class variable"

    Wow, I got that wrong? *Screams*
     
  10. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    However, that makes two errors: both OnCollisionEnter2D and OnCollisionExit2D ask for a message parameter of type Collision2D.
     
  11. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Seriously, one I have this down pat I know what I need to do. I just like the way I did my code and appreciate the extra help with this part.
     
  12. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    It does always give you a nice sense of accomplishment when you are able to create something and it works.

    Remember that the docs are your best friend.

    I thought that I may have gotten the whole Collision2D and Collider2D mixed up.

    Both functions should look like this if you haven't already got that far yet.
    Code (CSharp):
    1. void OnCollision2DEnter(Collision2D other) { }
    2. void OnCollision2DExit(Collision2D other) { }
     
  13. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Newest problem: CompareTag does NOT work with Collision2D.
     
  14. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    other.gameObject.CompareTag();
     
  15. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Alright, I totally threw out your idea and came up with this. No offense-- you did try to help me!

    PlayerController:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float move;
    7.     public float maxSpeed = 10f;
    8.     bool facingRight = true;
    9.    
    10.     Animator anim;
    11.    
    12.     public float jumpForce = 50f;
    13.     public float normalfall = 2f;
    14.     public float fastfall = 5f;
    15.     public bool grounded;
    16.     public bool dead;
    17.     public GameObject platform;
    18.  
    19.     void Start ()
    20.     {
    21.         anim = GetComponent<Animator>();
    22.        
    23.         this.rigidbody2D.gravityScale = normalfall;
    24.     }
    25.    
    26.     void Update()
    27.     {
    28.  
    29.         if (GameObject.Find("Platform Top").GetComponent<PlatformTop>().pressure1)
    30.         {
    31.             grounded = true;
    32.         }
    33.         else
    34.         {
    35.             grounded = false;
    36.         }
    37.  
    38.         if (grounded)
    39.         {
    40.             anim.SetBool("grounded", true);
    41.  
    42.             if (Input.GetButton("Down"))
    43.                 anim.SetBool("Crouch", true);
    44.             else
    45.                 anim.SetBool("Crouch", false);
    46.            
    47.             if (Input.GetButtonDown("Jump"))
    48.                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
    49.         }
    50.         else
    51.         {
    52.             anim.SetBool("grounded", false);
    53.  
    54.             if (Input.GetButton("Down"))
    55.                 this.rigidbody2D.gravityScale = fastfall;
    56.             else
    57.                 this.rigidbody2D.gravityScale = normalfall;
    58.         }
    59.        
    60.         move = Input.GetAxis("Horizontal");
    61.         anim.SetFloat("Speed", Mathf.Abs(move));
    62.        
    63.     }
    64.    
    65.     void FixedUpdate()
    66.     {
    67.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    68.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    69.        
    70.         if (move > 0 && !facingRight)
    71.             Flip();
    72.         else if (move < 0 && facingRight)
    73.             Flip();
    74.     }
    75.    
    76.     void Flip ()
    77.     {
    78.         facingRight = !facingRight;
    79.         Vector3 theScale = transform.localScale;
    80.         theScale.x *= -1;
    81.         transform.localScale = theScale;
    82.     }
    83. }
    84.  
    PlatformTop:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformTop : MonoBehaviour
    5. {
    6.     public bool pressure1;
    7.  
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if (other.gameObject.CompareTag("Player1"))
    11.         {
    12.             pressure1 = true;
    13.         }
    14.     }
    15.    
    16.     void OnCollisionExit2D(Collision2D other)
    17.     {
    18.         if (other.gameObject.CompareTag("Player1"))
    19.         {
    20.             pressure1 = false;
    21.         }
    22.     }
    23. }
     
  16. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Does jump work? Trying to add force to a rigidbody outside of FixedUpdate does nothing for me.
     
  17. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Yeah, it works fine. Let me check my script for a moment.

    Okay, so here's all the relevant information compiled into a convenient JumpScript complete with fast-fall:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JumpScript : MonoBehaviour
    5. {
    6.     public float jumpForce = 50f;
    7.     public float normalfall = 2f;
    8.     public float fastfall = 5f;
    9.     public float maxSpeed = 10f;
    10.     public float move;
    11.  
    12.     public bool grounded;
    13.  
    14.     Animator anim;
    15.  
    16.     void Start ()
    17.     {
    18.         anim = GetComponent<Animator>();
    19.        
    20.         this.rigidbody2D.gravityScale = normalfall;
    21.     }
    22.  
    23.     void Update ()
    24.     {
    25.         if (GameObject.Find("Platform Top").GetComponent<PlatformTop>().pressure1)
    26.         {
    27.             grounded = true;
    28.         }
    29.         else
    30.         {
    31.             grounded = false;
    32.         }
    33.        
    34.         if (grounded)
    35.         {
    36.             anim.SetBool("grounded", true);
    37.            
    38.             if (Input.GetButton("Down"))
    39.                 anim.SetBool("Crouch", true);
    40.             else
    41.                 anim.SetBool("Crouch", false);
    42.            
    43.             if (Input.GetButtonDown("Jump"))
    44.                 rigidbody2D.AddForce(new Vector2(0, jumpForce));
    45.         }
    46.         else
    47.         {
    48.             anim.SetBool("grounded", false);
    49.            
    50.             if (Input.GetButton("Down"))
    51.                 this.rigidbody2D.gravityScale = fastfall;
    52.             else
    53.                 this.rigidbody2D.gravityScale = normalfall;
    54.         }
    55.     }
    56.  
    57.     void FixedUpdate()
    58.     {
    59.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    60.         anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
    61.     }
    62. }
    63.  
    And its' buddy, PlatformTop.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformTop : MonoBehaviour
    5. {
    6.     public bool pressure1;
    7.  
    8.     void OnCollisionEnter2D(Collision2D other)
    9.     {
    10.         if (other.gameObject.CompareTag("Player1"))
    11.         {
    12.             pressure1 = true;
    13.         }
    14.     }
    15.    
    16.     void OnCollisionExit2D(Collision2D other)
    17.     {
    18.         if (other.gameObject.CompareTag("Player1"))
    19.         {
    20.             pressure1 = false;
    21.         }
    22.     }
    23. }
    This does not include horizontal movement and its' only purrpose is to allow the user to jump. My full script also allows horizontal movement but I took out that part to show you what I did.

    Funny how the student becomes the teacher, eh?