Search Unity

Issue with Raycasting and OnCollisionEnter2D

Discussion in 'Scripting' started by videoanime, Feb 26, 2015.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Hello fellas, how are you? Can somebody help me to understand my code and how I could make it to work correctly? Here it is:

    Code (CSharp):
    1. private bool piso;
    2.     private bool suelo;
    3.     public GameObject elemento;
    4.    
    5.     public Transform gdchk; //da la posicion de Finn en el suelo
    6.     float gdradius=0.7f; //radio del area que estara revisando si se toca suelo o no
    7.     private Semillero2 semillero;
    8.  
    9.     float distancia;
    10.     // Update is called once per frame
    11.    
    12.     void Update ()
    13.         {
    14.         RaycastHit2D hit = Physics2D.Raycast(transform.position-new Vector3(0,transform.localScale.y/2,0), Vector3.down);
    15.         //Debug.DrawRay(transform.position-new Vector3(0,transform.localScale.y/2,0),Vector3.down);
    16.        
    17.         if (hit.collider != null)
    18.             {
    19.             distancia = Mathf.Abs (hit.point.y-transform.position.y);
    20.             if (Mathf.Floor(Mathf.Abs(rigidbody2D.velocity.y)) == 0 && transform.position.y < 7.1 && Mathf.Floor(distancia) == 0)
    21.                 {
    22.                 elemento.rigidbody2D.isKinematic = true;
    23.                 }
    24.             }
    25.         }
    26.    
    27.     void FixedUpdate ()
    28.         {
    29.         piso = Physics2D.OverlapCircle (gdchk.position, gdradius, 1 << LayerMask.NameToLayer("suelo")); //es un booleano que sera true o false si toca el suelo o no
    30.  
    31.         if (piso || (Mathf.Floor(Mathf.Abs(rigidbody2D.velocity.y)) == 0 && transform.position.y < 7.1))
    32.             {
    33.             suelo = true;
    34.             Semillero2.pieza = false;
    35.             }
    36.         }
    37.    
    38.     void OnCollisionEnter2D(Collision2D coll)
    39.         {
    40.         if (!Semillero2.pieza && suelo)
    41.             {
    42.             if (Mathf.Floor(distancia) == 0)
    43.                 {
    44.                 elemento.rigidbody2D.isKinematic = true;
    45.                 semillero = Semillero2.instancia ();
    46.                 semillero.Spawn ();
    47.                 }
    48.             }
    49.         }
    Well, the thing is this, this script manages random pieces that appear at the top of the screen and then start falling down, so, supposedly, when one piece reaches the bottom, a new one is going to appear at the top again, as you can see I do this in my OnCollisionEnter2D, the problem is here precisely.

    You see that I need to have the variable "distancia" at zero to spawn a new one, I can stack 4 pieces in order to make a column, and then, with the fifth one I intentionally position it beside the column to know if one new piece is going to appear or not.

    The problem here is that when the fifth piece touches the topmost piece, it spawns a new piece, then when touches the next, it spawn other a so on, when the piece reaches the bottom, there were 5 new pieces, this is completely wrong. The correct would be that just one new piece should appear until the bottom was reached.

    I don't understand why this is happening, I'm constantly checking the variable "distancia" on my Update part, but for an incomprehensible reason it doesn't work as I hoped.

    Doesn't is supposed that a new piece shouldn't appear until the current one touches the bottom or touches other piece?

    I hope that someone comprehend my code and can give hints or explain to me this performance.

    Thank you.
     
  2. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    UPDATE: I fixed my problem, just adding the word static to my variable distancia. It seems that all the time distancia was zero since the beggining, that's why it spawned a piece for each that the falling down one touched.